Single Number
Every number appears twice except one. Find it in one pass with no extra memory, using XOR.
01The problem
In nums, every value appears exactly twice except one, which appears once. Return that single value. You must use O(n) time and O(1) extra space.
| Input | Output |
|---|---|
[2, 2, 1] | 1 |
[4, 1, 2, 1, 2] | 4 |
[1] | 1 |
02How to spot it
"Everything appears twice except one" plus "no extra memory" is the classic hint for XOR (^ in Java). A HashSet would work too, but it uses O(n) memory.
03Pseudocode
result = 0
for each n in nums:
result = result XOR n
return resultTwo facts make this work:
- x XOR x = 0: a number cancels itself out.
- x XOR 0 = x: XOR with zero changes nothing.
And XOR doesn't care about order, so every pair cancels, and only the single number is left.
04Watch it work
result
0
binary 000
Start with result = 0. XOR-ing anything with 0 leaves it unchanged.
Pseudocode
- result = 0
- for each n in nums:
- result = result XOR n
- return result
Variables
- result
- 0
05Step-by-step dry run
- 14011221324
result
0
binary 000
Start with result = 0. XOR-ing anything with 0 leaves it unchanged.
result = 0
- 24011221324i
result
4
000 ⊕ 100 = 100
Mix in 4: 0 ⊕ 4 = 4. Its bits are switched on in result for now.
n = 4result = 4
- 34011221324i
result
5
100 ⊕ 001 = 101
Mix in 1: 4 ⊕ 1 = 5. Its bits are switched on in result for now.
n = 1result = 5
- 44011221324i
result
7
101 ⊕ 010 = 111
Mix in 2: 5 ⊕ 2 = 7. Its bits are switched on in result for now.
n = 2result = 7
- 54011221324i
result
6
111 ⊕ 001 = 110
1 again: XOR flips the same bits back, so the pair cancels out. 7 ⊕ 1 = 6.
n = 1result = 6
- 64011221324i
result
4
110 ⊕ 010 = 100
2 again: XOR flips the same bits back, so the pair cancels out. 6 ⊕ 2 = 4.
n = 2result = 4
- 74011221324
result
4
binary 100
Every pair cancelled to 0. Only the lonely number survives: return 4.
result = 4
06Java solution
public class SingleNumber {
static int singleNumber(int[] nums) {
int result = 0;
for (int n : nums) result ^= n;
return result;
}
public static void main(String[] args) {
System.out.println(singleNumber(new int[] {2, 2, 1}));
System.out.println(singleNumber(new int[] {4, 1, 2, 1, 2}));
System.out.println(singleNumber(new int[] {-3, 7, 7}));
}
}1 4 -3
The tricky line: result ^= n is short for result = result ^ n. ^ in Java is XOR, not "power".
07Why it works
XOR the whole array in any order: 4 ^ 1 ^ 2 ^ 1 ^ 2 = 4 ^ (1 ^ 1) ^ (2 ^ 2) = 4 ^ 0 ^ 0 = 4. Each pair becomes 0, and 0 changes nothing.
08Time and space
- Time: O(n). One pass.
- Space: O(1). One
int.
09Edge cases
| Case | Result |
|---|---|
| One element | That element |
| Negative numbers | Work: XOR acts on the bits |
| Single number is 0 | Returns 0 correctly |
10Common mistakes
- Thinking
^means power. In Java,2 ^ 3is1, not8. UseMath.powfor powers. - Starting
resultatnums[0]and looping from 0. Thennums[0]is XOR-ed twice and disappears.
11Other approaches
| Approach | Time | Extra space |
|---|---|---|
| HashSet add/remove | O(n) | O(n) |
| Sort, check pairs | O(n log n) | O(1) |
| XOR | O(n) | O(1) |
12In the interview
13Check your understanding
Quiz · 2 questions
0 of 2 answered
1. What is 5 ^ 5 in Java?
2. Why does the order of numbers not matter?
14Solve it yourself
Try it on LeetCode #136.