Single Element in a Sorted Array
Single Element in a Sorted Array is LeetCode 540 (Medium). The array is sorted, every value appears exactly twice except one value that appears once. Return that value.
- The solution must run in O(log n) time and O(1) extra space.
That rules out XOR-ing everything or scanning pairs, which both read all n elements.
- 1 <= nums.length <= 10⁵
- 0 <= nums[i] <= 10⁵
Intuition
Look at the array as pairs starting at even indices: (0,1), (2,3), (4,5) and so on.
- Before the single element, every pair is intact:
nums[2k] == nums[2k+1]. - From the single element on, everything has shifted by one, so
nums[2k]no longer equalsnums[2k+1].
So the question "is the pair at this even index intact?" is yes up to some point and no after it. That turns the problem into a binary search for the first broken pair, and the single element is the first element of that pair.
No target value to compare against, but a yes/no question that is true on one side of the answer and false on the other. That shape means binary search on the condition, even though nothing here is a target.
Approach
Before reading on, write [1,1,2,3,3,4,4] with indices and circle each pair that starts at an even index. Which pairs are intact, and where does the first broken one start?
Search the whole array
Set lo = 0 and hi = n - 1. The single element is always inside [lo, hi], and the loop runs while lo < hi, so it stops when one index is left.
Move mid to an even index
Compute mid and, if it is odd, subtract 1. Pairs only start at even indices before the single element, so testing from an even index is what makes "intact or broken" meaningful.
Intact pair: go right past it
If nums[mid] == nums[mid + 1], everything up to mid + 1 is still correctly paired, so the single element is to the right. Set lo = mid + 2; both elements of the pair are excluded.
Broken pair: keep mid
Otherwise the shift has already happened at or before mid, so set hi = mid. mid itself may be the single element, so it must stay in the range.
Return the last index left
When lo == hi, that index holds the single element. mid + 1 is always valid inside the loop: mid is even and below hi, and hi is always even too, so mid + 1 <= hi.
Code in Python, C++ and Java
Common pitfalls
Comparing from an odd mid
mid = (lo + hi) // 2
if nums[mid] == nums[mid + 1]:
lo = mid + 2mid = (lo + hi) // 2
if mid % 2 == 1:
mid -= 1
if nums[mid] == nums[mid + 1]:
lo = mid + 2An odd index is the second half of a pair, so nums[mid] and nums[mid + 1] come from two different pairs and their equality means nothing. On [1,2,2], mid = 1 sees 2 == 2, jumps to lo = 3, and reads past the end of the array.
Looping while lo <= hi
while lo <= hi:
while lo < hi:
With <= the loop runs once more when one index is left. On [1,1,2], lo and hi both reach 2, mid is 2, and nums[mid + 1] is index 3, outside the array.
Edge cases
[7]lo == hi from the start, so the loop never runs and nums[0] is returned.
[1,1,2,2,3]Every tested pair is intact, so lo keeps moving right until it reaches index 4. The loop ends there and returns 3.
Complexity
[lo, hi], and only two indices and one comparison are used per step.Single Element in a Sorted Array FAQ
Why is the single element always at an even index?
Everything before it is made of complete pairs, so the number of elements before it is even. Its index equals that count, so it is even. The same count argument shows n is odd, so hi = n - 1 starts even.
What is the mid ^ 1 trick?
mid ^ 1 flips the lowest bit, giving mid + 1 for even mid and mid - 1 for odd mid: the index of its partner in a healthy pair. Testing nums[mid] == nums[mid ^ 1] replaces the even-adjust step; on a match set lo = mid + 1, otherwise hi = mid.
Would XOR of all elements work?
It gives the right value, because each pair cancels to 0, and it does not even need the array to be sorted. But it reads every element, so it is O(n) and does not meet the O(log n) requirement.