Every value appears twice except one — find it with XOR in O(n) time and O(1) space. Run it in Python and compare against a Counter solution.
The problem
Every value in nums appears exactly twice, except one value that appears once. Return that value.
Bonus: use O(1) extra memory.
Examples
Example 1
Input
single_number([4, 1, 2, 1, 2, 4, 7])
Expected output
7
Example 2
Input
single_number([2, 2, 1])
Expected output
1
+ 3 hidden tests on Submit — negative single, zero is the answer.
Edge cases to ask about
- Single element
- Answer is 0
- Negative numbers
Hints
0/3How an interviewer scores this
0/9Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.
Complexity Lab
What does this cost as n grows?
Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.
Pick both to reveal the answer.
Measure it
Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.
From brute force to optimal
The progression an interviewer wants to hear, one step at a time.
| Approach | Time | Space | Idea |
|---|---|---|---|
| Count occurrences | O(n) | O(n) | Counter, then the key with count 1. |
| bestXOR everything | O(n) | O(1) | x ^ x = 0 and x ^ 0 = x, so pairs cancel and the single value survives. |
Walkthrough of the optimal approach (try it yourself first)
XOR has two properties that make this a one-liner: x ^ x == 0 and x ^ 0 == x, and it is commutative. XOR all values together: every pair cancels to 0, leaving only the value that appeared once.
The Counter version is perfectly correct and O(n) — XOR is the answer to the follow-up "now without extra memory".
Complexity: O(n) time, O(1) space. One pass with a single integer accumulator — XOR needs no table.
Reveal the reference solution
def single_number(nums): result = 0 for x in nums: result ^= x return result
The brute force, for comparison
def single_number(nums): from collections import Counter for x, c in Counter(nums).items(): if c == 1: return x
Follow-ups interviewers ask
- Every value appears three times except one.
- Two values appear once — find both.
Frequently asked interview questions
Core interview concepts, complexities, and follow-ups scored by hiring teams.
What is the time complexity of Find the Element That Appears Once in Python?
The optimal solution runs in O(n) time and O(1) auxiliary space. One pass with a single integer accumulator — XOR needs no table.
What is the brute-force approach, and how do you optimise it?
Count occurrences: O(n) time, O(n) space. Counter, then the key with count 1. XOR everything: O(n) time, O(1) space. x ^ x = 0 and x ^ 0 = x, so pairs cancel and the single value survives.
What follow-up questions do interviewers ask about Find the Element That Appears Once?
Every value appears three times except one. Two values appear once — find both.
