Browse curriculum

Single Element in a Sorted Array

Find the one value that appears once in a sorted array of pairs in O(log n): binary search on whether the pair at an even index is still intact.

Problem Understanding

Single Element in a Sorted Array: in a sorted array, every element appears exactly twice except one, which appears once. Return that single element in O(log n) time and O(1) space.

Examples: [1,1,2,3,3,4,4,8,8] returns 2; [3,3,7,7,10,11,11] returns 10.

The Intuition: The Single Shifts the Pairs

Look at where pairs start:

  1. Left of the single, pairs sit at (0,1), (2,3), (4,5), ...: every pair starts at an even index.
  2. Right of the single, everything is shifted by one, so pairs start at odd indexes.
  3. So at an even mid, nums[mid] == nums[mid + 1] means "still aligned": the single is to the right. Otherwise it is at mid or to the left.

Each comparison halves the range, a binary search on a yes/no property rather than on the values themselves, like Find Peak Element and First Bad Version. Without the sorted order, the XOR trick from Single Number would be the best you could do, in O(n).

Interactive Walkthrough

mid IS ALWAYS EVEN · TINTED = mid + 1

FULL RANGE

lo

1

0

1

1

2

2

3

3

3

4

4

5

4

6

8

7

hi

8

8

lo

0

hi

8

single

:

 

lo = 0, hi = 8. Before the single, pairs start at even indexes; after it, at odd ones

Next

mid = 4. nums[4] = 3 ≠ nums[5] = 4: the alignment broke at or before 4. hi = 4

Free preview: the first 3 of 5 steps

nums

The array shows lo, mid and hi; cells outside the range are struck out. mid is solid and its partner mid + 1 is tinted. Try Single first to see every check send the search left.

Dry Run Table

Input: [1,1,2,3,3,4,4,8,8]

lo hi mid (even) Pair Move
0 8 4 3 ≠ 4 hi = 4
0 4 2 2 ≠ 3 hi = 2
0 2 1 → 0 1 = 1 lo = 2

Answer: lo = hi = 2, so nums[2] = 2.

The Solution Template

Binary search with lo < hi. Moving lo past the whole intact pair (mid + 2) keeps lo on an even index.

Single Element in a Sorted Array Code
1var singleNonDuplicate = function(nums) {
2 let lo = 0, hi = nums.length - 1;
3 while (lo < hi) {
4 let mid = Math.floor((lo + hi) / 2);
5 if (mid % 2 === 1) mid--;
6 if (nums[mid] === nums[mid + 1]) lo = mid + 2;
7 else hi = mid;
8 }
9 return nums[lo];
10};

Edge Cases & Common Mistakes

  • One element: lo == hi from the start; return it.
  • Single at either end: works without special cases; the search just keeps moving one way.
  • Comparing at an odd mid: nums[mid] == nums[mid + 1] then means the opposite; snap to even first (or compare with mid ^ 1).
  • lo = mid + 1: lands on an odd index and breaks the alignment rule; skip the whole pair with mid + 2.
  • XOR or a hash map: correct but O(n).

Summary

Pairs start at even indexes before the single and odd ones after. Snap mid to even; if nums[mid] == nums[mid + 1], move lo to mid + 2, otherwise hi = mid. Return nums[lo]. O(log n) time, O(1) space.

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Search the whole array

    lo = 0, hi = n - 1.

  2. Check the pair at an even mid

    mid = (lo + hi) / 2, minus 1 if odd. If nums[mid] == nums[mid + 1], set lo = mid + 2; otherwise hi = mid.

  3. Return

    When lo == hi, nums[lo] is the single element.

Frequently asked questions

Why does binary search work for Single Element in a Sorted Array?

Before the single element, every pair starts at an even index (0-1, 2-3, ...). The single shifts everything after it by one, so later pairs start at odd indexes. Checking whether the pair starting at an even mid is intact tells you which side the single is on.


Why snap mid to an even index?

The test compares nums[mid] with nums[mid + 1], which only means "the pair is intact" when mid is where a pair should start, an even index. Subtracting 1 from an odd mid keeps the check simple. Another common form is to compare nums[mid] with nums[mid ^ 1].


Why not just XOR everything?

XOR of all values does return the single element, because pairs cancel, but it reads every element: O(n). The problem asks for O(log n), which only the sorted order makes possible.

New to the technique behind this problem? Watch it run step by step:

Binary Search Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description