Single Element in a Sorted Array

Medium

Topics
ArrayBinary Search

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

101122333445468788
Everything is paired except index 2.
Cells are the array values; the small numbers underneath are indices.
Input:  nums = [1,1,2,3,3,4,4,8,8]
Output: 2

Example 2

30317273104115116
Input:  nums = [3,3,7,7,10,11,11]
Output: 10

Example 3

10
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

  1. Set lo = 0, hi = len(nums) - 1.
  2. Take mid and decrement it by one if it is odd, making it the start of a pair.
  3. If nums[mid] == nums[mid+1] the prefix is intact, so lo = mid + 2.
  4. Otherwise hi = mid.
  5. 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] = 2

Complexity

  • 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 mid to 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 + 1 instead of mid + 2. After confirming the pair at mid is intact, both of its indices are excluded.
  • Snapping with mid -= mid % 2 and then forgetting that mid + 1 must stay in range. It does, because mid < hi and hi is a valid index, but the reasoning is worth stating.
  • Comparing nums[mid] with nums[mid-1] instead, which needs a different parity convention and usually produces an off-by-one.
  • Returning lo rather than nums[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]
Asked at Snowflake Zomato
SensAI
Beta
Tap the mic to stop voice mode