Single Element in a Sorted Array
Medium
Topics
Given a sorted array nums where every element appears exactly twice except for one element that appears exactly once, return the single element. 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
Example 3
Input: nums = [1] Output: 1
Constraints
- 1 <= nums.length <= 10^5
- 0 <= nums[i] <= 10^5
- Every element appears twice except one which appears once.
Editorial — intuition, approach, complexity and pitfalls
Intuition
Before the lone element, every pair starts at an even index: positions 0–1, 2–3, and so on. After it, that alignment is broken and pairs start at odd indices. So "is the pair starting at this even index still intact" is a monotone predicate, and binary search finds where it first fails.
Forcing mid down to an even index is what makes the test meaningful — it always asks about the start of a candidate pair. If nums[mid] == nums[mid+1] the alignment still holds, so the single element is further right; otherwise it is at mid or to its left.
Approach
- Set
lo = 0,hi = len(nums) - 1. - Take
midand decrement it by one if it is odd, making it the start of a pair. - If
nums[mid] == nums[mid+1]the prefix is intact, solo = mid + 2. - Otherwise
hi = mid. - Return
nums[lo].
Dry Run
nums = [1,1,2,3,3,4,4,8,8]
index 0 1 2 3 4 5 6 7 8
lo hi mid (even) nums[mid] nums[mid+1] pair intact meaning
0 8 4 3 4 no single is here or left
0 4 2 2 3 no single is here or left
0 2 0 1 1 yes single is right of the pair
lo == hi == 2 answer nums[2] = 2Complexity
- Time — O(log n): the range halves each step. The problem names this bound explicitly.
- Space — O(1), also named explicitly.
Common Pitfalls
- XOR-ing the whole array. It is a lovely O(n) trick and it is the wrong answer here, because the problem demands O(log n).
- Not snapping
midto an even index. The pair test then asks about the middle of a pair half the time and the predicate stops being monotone. - Using
lo = mid + 1instead ofmid + 2. After confirming the pair atmidis intact, both of its indices are excluded. - Snapping with
mid -= mid % 2and then forgetting thatmid + 1must stay in range. It does, becausemid < hiandhiis a valid index, but the reasoning is worth stating. - Comparing
nums[mid]withnums[mid-1]instead, which needs a different parity convention and usually produces an off-by-one. - Returning
lorather thannums[lo]. The question asks for the element, not its index.
Full Solution — all languages
class Solution:
def singleNonDuplicate(self, nums):
lo, hi = 0, len(nums) - 1
while lo < hi:
mid = (lo + hi) // 2
if mid % 2 == 1:
mid -= 1
if nums[mid] == nums[mid + 1]:
lo = mid + 2
else:
hi = mid
return nums[lo]import java.util.*;
class Solution {
public int singleNonDuplicate(int[] nums) {
int lo = 0, hi = nums.length - 1;
while (lo < hi) {
int mid = (lo + hi) / 2;
if (mid % 2 == 1) mid--;
if (nums[mid] == nums[mid + 1]) lo = mid + 2;
else hi = mid;
}
return nums[lo];
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int singleNonDuplicate(vector<int>& nums) {
int lo = 0, hi = (int)nums.size() - 1;
while (lo < hi) {
int mid = (lo + hi) / 2;
if (mid % 2 == 1) mid--;
if (nums[mid] == nums[mid + 1]) lo = mid + 2;
else hi = mid;
}
return nums[lo];
}
};/**
* @param {number[]} nums
* @return {number}
*/ var singleNonDuplicate = function(nums) {
let lo = 0, hi = nums.length - 1;
while (lo < hi) {
let mid = (lo + hi) >> 1;
if (mid % 2 === 1) mid--;
if (nums[mid] === nums[mid + 1]) lo = mid + 2;
else hi = mid;
}
return nums[lo];
};