Skip to content

Latest commit

 

History

History
49 lines (36 loc) · 1.21 KB

_540. Single Element in a Sorted Array.md

File metadata and controls

49 lines (36 loc) · 1.21 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 05, 2024

Last updated : July 05, 2024


Related Topics : Array, Binary Search

Acceptance Rate : 59.29 %


Solutions

Python

class Solution:
    def singleNonDuplicate(self, nums: List[int]) -> int:
        left, right = 0, len(nums) - 1

        while left < right :
            mid = (left + right) // 2
            if nums[mid - 1] != nums[mid] != nums[mid + 1] :
                return nums[mid]

            if mid % 2 == 1 :
                if nums[mid] == nums[mid - 1] :
                    # is to right 
                    left = mid + 1
                else :
                    right = mid - 1
            elif nums[mid] == nums[mid + 1] :
                left = mid + 1
            else :
                right = mid - 1
        
        return nums[left]