540. Single Element in a Sorted Array - Detailed Explanation
Problem Statement
You are given a sorted array of integers where every element appears exactly twice except for one element that appears only once. Your task is to find that single element that does not have a duplicate.
Example 1
- Input:
nums = [1,1,2,3,3,4,4,8,8] - Output:
2 - Explanation:
In the array, every number appears twice except for2, which appears only once.
Example 2
- Input:
nums = [3,3,7,7,10,11,11] - Output:
10 - Explanation:
All elements appear in pairs except for10.
Constraints
- The array is sorted in non-decreasing order.
- Every element except one appears exactly twice.
- The array length is odd.
Hints and Intuition
-
Hint 1:
A straightforward approach is to use the XOR operation. Since (a \oplus a = 0) and XOR is commutative, XOR-ing all numbers will cancel out the numbers that appear twice, leaving the single number. -
Hint 2:
Because the array is sorted, you can use a binary search approach to achieve a more efficient solution (O(log n)). Notice that before the single element, pairs start at even indices; after the single element, the pairing order flips. Use this observation to decide which half of the array to search. -
Hint 3:
Check the index of the mid element:- If the mid index is even and its neighbor (mid + 1) is the same, the single element must be in the right half.
- Otherwise, it’s in the left half (including possibly at mid).
Detailed Approaches
Approach A: XOR (Brute Force)
- Idea:
XOR all elements together. The duplicates cancel out, and the result is the single element. - Pros:
Very simple and runs in O(n) time with O(1) space. - Cons:
Although linear, it doesn’t take advantage of the fact that the array is sorted.
Approach B: Binary Search (Optimal)
- Idea:
Use binary search to locate the single element by leveraging the sorted order:- Set
leftto 0 andrightto the last index. - Compute
midas the middle index. To ensure comparisons are made on the first element of a pair, adjustmidto be even. - If
nums[mid]equalsnums[mid+1], it means the pair is valid and the single element is on the right side; movelefttomid + 2. - Otherwise, the single element lies on the left (or could be at
mid), so moverighttomid. - Continue until
leftequalsright, which will be the single element.
- Set
- Pros:
Takes advantage of the sorted property, achieving O(log n) time complexity. - Cons:
Requires careful index handling and even/odd adjustments.
Step-by-Step Walkthrough (Binary Search)
Consider Example 1: nums = [1,1,2,3,3,4,4,8,8].
-
Initialization:
left = 0,right = 8.
-
First Iteration:
- Compute
mid = (0 + 8) // 2 = 4. - Make sure
midis even; if it’s not, adjust it. In this case, 4 is even. - Compare
nums[4]andnums[5]: they are3and4respectively, so they are not equal. - This means the single element lies to the left of
mid(includingmid), so setright = mid = 4.
- Compute
-
Second Iteration:
- Now,
left = 0andright = 4. - Compute
mid = (0 + 4) // 2 = 2(even). - Compare
nums[2]andnums[3]: they are2and3. Since they are not equal, the single element is atmidor to the left. - Set
right = mid = 2.
- Now,
-
Third Iteration:
- Now,
left = 0andright = 2. - Compute
mid = (0 + 2) // 2 = 1. Since1is odd, adjustmidto0(the even index). - Compare
nums[0]andnums[1]: they are both1, so the single element is to the right. - Set
left = mid + 2 = 2.
- Now,
-
Conclusion:
- Now,
left == right == 2. - Return
nums[2], which is2.
- Now,
Code Implementation
Python Implementation (Binary Search)
Java Implementation (Binary Search)
Complexity Analysis
Binary Search Approach
- Time Complexity:
The algorithm uses binary search, which runs in O(log n) time. - Space Complexity:
The algorithm uses O(1) extra space.
XOR Approach (Alternate)
- Time Complexity:
The XOR approach would run in O(n) time. - Space Complexity:
It requires O(1) extra space.
Common Pitfalls and Edge Cases
Common Pitfalls:
- Incorrect Index Handling:
Not adjusting the middle index to be even can lead to comparing mismatched pairs. - Off-by-One Errors:
Ensure that the pointers are updated correctly in each iteration.
Edge Cases:
- Single Element Array:
When the array has only one element, it should be returned immediately. - All Pairs Valid Except Single Element:
The binary search method should correctly identify the switch point where the pairing order flips.
Related Problems for Further Practice
-
Find the Duplicate Number:
Another binary search problem that uses sorted properties and counting. -
Search in Rotated Sorted Array:
A problem that involves using binary search on a modified sorted array. -
Majority Element:
Identifying the element that appears most frequently using linear or logarithmic approaches.
GET YOUR FREE
Coding Questions Catalog
$197

$78
$78