Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.
- Return the single element that appears only once.
- """
- class Solution:
- def singleNonDuplicate(self, nums: List[int]) -> int:
- left, right = 0, len(nums)-1
- while left < right:
- mid = left + (right - left) // 2
- """
- Assuming there is no odd-one-out-element e.g [1,1,2,2,3,3],
- if mid = 0 -> its duplicate is at mid = mid + 1
- if mid = 1 -> its duplicate is at mid = mid - 1
- """
- if ( (mid % 2 == 1 and nums[mid - 1] == nums[mid]) or
- (mid % 2 == 0 and nums[mid] == nums[mid + 1]) ):
- left = mid + 1
- else:
- right = mid
- return nums[left]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement