Single Element in a Sorted Array

medium

You are given a sorted array of integers nums in which every element appears exactly twice — except for one element, which appears exactly once.

Return the element that appears only once.

Your algorithm must run in O(log n) time and use O(1) extra space.

Hints

The required O(log n) is a giant signpost: some yes/no question about the middle of the array must tell you which half holds the answer — and it is not a comparison of values.
Look at indices, not values. Before the single element, the first copy of every pair sits at an even index. What happens to that alignment after the single element appears?
At an even index i, test nums[i] == nums[i+1]. It is true for every even index before the single element and false from it onward — binary search that flip point, keeping mid on even indices and moving lo = mid + 2 or hi = mid.

Common doubts

The array length is odd (pairs contribute an even count, plus one). Every pair before the single element occupies an (even, odd) slot, so the single element lands on the next even index — and every pair after it starts on an odd index.
Then that element is the answer. In the binary search, lo == hi from the start, the loop never runs, and nums[0] is returned immediately.
XOR of all elements does cancel the pairs and leave the single one — but it reads every element, so it is O(n). The whole point of this problem is beating that with O(log n) by exploiting the sorted order.
No. While lo < hi, the midpoint (after stepping down to even) is strictly less than hi, so nums[mid + 1] is always in bounds.

Interview follow-ups

XOR all elements — pairs cancel and the single one survives, in O(n) time and O(1) space. Without order there is no structure to binary search, so O(n) is optimal.
Plain XOR no longer cancels. Count set bits per position modulo 3, or use the two-mask ones/twos trick — the Single Number II pattern. On a sorted array you could still binary search over index alignment in blocks of three.
XOR everything to get x XOR y, split elements into two groups by any set bit of that result, and XOR each group — the Single Number III pattern. In a sorted array, a pair-walk finds both in one pass.

Fun facts

  • The binary search here never asks which value is bigger — it only asks are these two neighbors equal. It is a search over index parity, not magnitude.
  • The skeleton — a guarantee that is intact on a prefix and broken on the suffix, binary searched at its flip point — is the same one that solves finding the first bad version, searching rotated arrays, and finding a peak element.

Asked at

AmazonGoogleMicrosoftAdobe
Frequently Sometimes Occasionally
Example 1
Input: nums = [1,1,2,3,3,4,4,8,8]
Output: 2
Every value pairs up except 2 — 1, 3, 4, and 8 all appear twice.
Example 2
Input: nums = [3,3,7,7,10,11,11]
Output: 10
10 is the only value with no partner.
Constraints

- 1 <= nums.length <= 10^5 - 0 <= nums[i] <= 10^5 - nums is sorted in non-decreasing order - Every element appears exactly twice, except for one element which appears exactly once

Solve this problem →