Single Number
Start with ans = 0 and XOR every number into it. Two equal numbers cancel, because x ^ x = 0. Zero changes nothing, because x ^ 0 = x, so the single value stays. The order does not matter, so the pairs do not need to sit next to each other.
The running value is valid when it equals the XOR of every number read so far. At the end, every repeated value has appeared twice and cancelled, so only the single value is left. Pairs cancel out, whatever the order.
Problem
You get an array nums. Every value in it appears twice, except for one value that appears only once. Return that single value.
The numbers are in no particular order. Your solution must run in linear time and use constant extra space.
An integer array nums with at least one value. Every value appears twice except one.
The one value that appears exactly once.
The 2 shows up twice, so the value with no partner is 1.
1 and 2 each show up twice. The 4 is the only value without a partner.
There is only one value, so it is the single one.
- • 1 ≤ nums.length ≤ 3 · 10^4
- • −3 · 10^4 ≤ nums[i] ≤ 3 · 10^4
- • Every value appears twice, except one value that appears once.
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
If I counted how many times each value appears, which count would I be looking for?
- 2.
Can I answer with a simple yes/no check, like 'is this value in the array?' Why or why not?
- 3.
What does XOR give when I combine a number with itself? And with 0?
- 4.
If I XOR every number in the array together, which numbers should disappear?
- 5.
Does the order of the numbers change the result of a chain of XORs?
Brute force approach
Better approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | For each value, scan the array and count it; return the one with count 1 | O(N²) | O(1) |
| Better | Count every value once in a hash map, then return the value with count 1 | O(N) | O(N) |
| Optimal | XOR every number into one running value; the pairs cancel and the single one is left | O(N) | O(1) |
Why does this belong to this pattern?
- “every element appears twice except one”Values come in pairs, so pairs must cancel out. That is what XOR does.
- “linear time, constant extra space”No hash map and no sorting. Read the array once, with one running value.
- “find the one that appears once”The answer is whatever is left after every pair has cancelled.
This is the anchor for the XOR sub-pattern. XOR works because a pair cancels itself and 0 is the neutral value. The next questions change the setup. Decode XORed Array uses XOR to undo one step. Single Number III has two singles, so one XOR total mixes them together. Single Number II has triples, which XOR does not cancel.
Don't make this mistake
This is the pattern-confusion trap. On [2, 2, 3, 2] the XOR is 2 ^ 2 ^ 3 ^ 2 = 1, but the answer is 3. The first two 2s cancel, leaving 3, and the third 2 XORs it to 1. Three equal values do not cancel: x ^ x ^ x = x.
nums[0] gets XORed in twice, once as the start and once in the loop, so it cancels itself. On [4, 1, 2, 1, 2] this gives 4 ^ 4 ^ 1 ^ 2 ^ 1 ^ 2 = 0, not 4. Starting at 1 is also wrong: it adds a 1 to the result (1 ^ 4 = 5). The neutral start is 0.
This only works if equal values sit next to each other, which needs a sort first (O(N log N)). On [1, 2, 1, 2, 4] the first two numbers differ, so it returns 1. The answer is 4.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Bit Manipulation translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.