Single Number
Problem
You are given an array of integers in which every value appears exactly twice except for one value, which appears exactly once. Find that value. Your solution must run in linear time and must not allocate memory proportional to the input size.
- The array holds at most 30,000 elements.
- Values fit comfortably in a 32-bit signed range.
- Exactly one element appears once; every other element appears exactly twice.
Examples
nums = [5, 5, 9]→9.nums = [8, 3, 6, 3, 6]→8; the pairs3,3and6,6cancel out.nums = [-12]→-12; a single element is trivially the answer, and negatives are allowed.
Hints
- A hash map of counts solves it easily, but it uses O(n) space. What operation "cancels out" two equal numbers?
- Look for an operation that is commutative and associative, so order does not matter, and where
x op xis the identity. - XOR all the numbers together: pairs cancel to 0, and
0 ^ x = x.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.