Watch 10 video solutions for Single Element in a Sorted Array, a medium level problem involving Array, Binary Search. This walkthrough by Techdose has 58,400 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 O(log n) time and O(1) space.
Example 1:
Input: nums = [1,1,2,3,3,4,4,8,8] Output: 2
Example 2:
Input: nums = [3,3,7,7,10,11,11] Output: 10
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 105Problem Overview: You are given a sorted array where every element appears exactly twice except one element that appears once. The task is to return that single element. The sorted property creates a predictable pairing pattern that allows more efficient solutions than a full scan.
Approach 1: Bitwise XOR (O(n) time, O(1) space)
The simplest solution relies on the XOR property: a ^ a = 0 and a ^ 0 = a. Iterate through the array and XOR every value with an accumulator. Since every duplicated element cancels itself out, the final value left in the accumulator is the single element. This approach ignores the sorted nature of the array and works for any array where every number appears twice except one. Implementation is extremely short and constant space, but the runtime is linear because you must scan every element.
Approach 2: Binary Search (O(log n) time, O(1) space)
The optimal solution leverages the sorted structure of the array together with binary search. In a correctly paired array, elements appear in pairs where the first index is even and the second index is odd (e.g., [a,a,b,b,c,c]). Once the single element appears, this pairing pattern breaks. During binary search, check whether mid forms a valid pair with its neighbor. If mid is even and nums[mid] == nums[mid+1], the single element lies to the right; otherwise it lies to the left. If mid is odd, compare with nums[mid-1]. Each step discards half of the search space, reducing the runtime to logarithmic time.
The key insight is that the single element shifts the index parity of every pair after it. Binary search detects where that shift occurs. Because the array is sorted and duplicates are adjacent, each comparison immediately tells you which half of the array still follows the valid pairing structure.
Recommended for interviews: The binary search solution is what interviewers typically expect because it exploits the sorted property and achieves O(log n) time. The XOR approach is still useful to mention first—it demonstrates awareness of bitwise tricks and works in the general case—but the optimized binary search shows stronger algorithmic reasoning and pattern recognition.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Bitwise XOR | O(n) | O(1) | When the array may not be sorted or you want the simplest constant-space scan |
| Binary Search | O(log n) | O(1) | Best choice when the array is sorted and duplicates appear in pairs |