Problem
Section titled “Problem”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.
Your solution must run in time and space.
Example 1:
Input: nums = [1,1,2,3,3,4,4,8,8]Output: 2Example 2:
Input: nums = [3,3,7,7,10,11,11]Output: 10Constraints:
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”Look at where each pair starts. Before the single element, every pair occupies indices (even, odd): nums[0] == nums[1], nums[2] == nums[3], and so on. The single element shifts everything after it by one, so after it every pair occupies (odd, even).
So for any even index mid:
- if
nums[mid] == nums[mid + 1], the pairing is still “normal” atmid, and the single element lies to the right ofmid + 1; - otherwise the pairing is already broken, and the single element is at
midor to its left.
That predicate is monotonic over the even indices, so we can binary search on it. The array length is odd (pairs plus one), so the search range [lo, hi] always has even endpoints and the answer is the first even index where the pairing breaks.
Algorithm
Section titled “Algorithm”- Set
lo = 0,hi = n - 1(both even, sincenis odd). - While
lo < hi:mid = lo + (hi - lo) / 2; ifmidis odd, decrement it so that it is even (it stays>= lo).- If
nums[mid] == nums[mid + 1], the single element is after this pair:lo = mid + 2. - Otherwise it is at
midor before:hi = mid.
- Return
nums[lo].
The invariant is that the single element is always at an even index in [lo, hi]. Since mid < hi, mid + 1 is always a valid index.
Walkthrough
Section titled “Walkthrough”Blue is mid, orange is its partner mid + 1, grey indices have been ruled out:
Complexity
Section titled “Complexity”- Time: — the range
[lo, hi]at least halves every iteration. - Space: — three indices.
Edge cases
Section titled “Edge cases”n == 1:lo == hi == 0, the loop never runs andnums[0]is returned.- Single element first or last (e.g.
[1,2,2]or[1,1,2]): the search converges to index0orn - 1respectively; no special case is needed.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { public: int singleNonDuplicate(vector<int>& nums) { int lo = 0, hi = (int)nums.size() - 1; while (lo < hi) { int mid = lo + (hi - lo) / 2; if (mid % 2 == 1) mid--; // compare from the start of a pair if (nums[mid] == nums[mid + 1]) { lo = mid + 2; } else { hi = mid; } } return nums[lo]; }};class Solution { public int singleNonDuplicate(int[] nums) { int lo = 0, hi = nums.length - 1; while (lo < hi) { int mid = lo + (hi - lo) / 2; if (mid % 2 == 1) mid--; // compare from the start of a pair if (nums[mid] == nums[mid + 1]) { lo = mid + 2; } else { hi = mid; } } return nums[lo]; }}class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: lo, hi = 0, len(nums) - 1 while lo < hi: mid = (lo + hi) // 2 if mid % 2 == 1: mid -= 1 # compare from the start of a pair if nums[mid] == nums[mid + 1]: lo = mid + 2 else: hi = mid return nums[lo]Give it a try first
Attempt the problem on LeetCode — the code will still be here.
![Binary search on pairs for [1,1,2,3,3,4,4,8,8], step 1 of 8](/CodePrime/img/codes/single-element-in-a-sorted-array/pairs/01.webp)
![Binary search on pairs for [1,1,2,3,3,4,4,8,8], step 2 of 8](/CodePrime/img/codes/single-element-in-a-sorted-array/pairs/02.webp)
![Binary search on pairs for [1,1,2,3,3,4,4,8,8], step 3 of 8](/CodePrime/img/codes/single-element-in-a-sorted-array/pairs/03.webp)
![Binary search on pairs for [1,1,2,3,3,4,4,8,8], step 4 of 8](/CodePrime/img/codes/single-element-in-a-sorted-array/pairs/04.webp)
![Binary search on pairs for [1,1,2,3,3,4,4,8,8], step 5 of 8](/CodePrime/img/codes/single-element-in-a-sorted-array/pairs/05.webp)
![Binary search on pairs for [1,1,2,3,3,4,4,8,8], step 6 of 8](/CodePrime/img/codes/single-element-in-a-sorted-array/pairs/06.webp)
![Binary search on pairs for [1,1,2,3,3,4,4,8,8], step 7 of 8](/CodePrime/img/codes/single-element-in-a-sorted-array/pairs/07.webp)
![Binary search on pairs for [1,1,2,3,3,4,4,8,8], step 8 of 8](/CodePrime/img/codes/single-element-in-a-sorted-array/pairs/08.webp)