0
xor
Every element appears exactly twice except for one — find that one in O(n) time and O(1) space. The trick is a two-line XOR fold: identical pairs cancel to 0, leaving the lone value standing.
Given a non-empty array of integers where every element appears twice except for one, return the element that appears only once. No extra memory allowed.
Example: [4, 1, 2, 1, 2] → 4. The pairs (1,1) and (2,2) cancel; 4 has no partner.
a ^ a = 0 (any value XORed with itself vanishes) and 0 ^ a = a (zero is the identity). XOR every element in the array — all duplicates cancel to 0, and the surviving xor value is the single number. Order does not matter because XOR is commutative and associative.xor = 0. This is the XOR identity — it will absorb the first real element unchanged.n in the array, compute xor ^= n. Every pair (a, a) that appears contributes a ^ a = 0 and collapses away.xor holds exactly the value that had no pair. Return it. No extra data structure needed.At each bit position, an even number of 1s XORs to 0 and an odd number XORs to 1. Because every duplicate contributes an even count of 1s per bit, only the single number's bits survive.
0; the lone value survives.xor = 0. Input: [4, 1, 2, 1, 2]1▶function singleNumber(nums: number[]): number {2▶ let xor = 0;3 for (const n of nums) {4 xor ^= n; // pairs cancel: a ^ a = 0; identity: 0 ^ a = a5 }6 return xor; // only the unpaired element remains7}
function singleNumber(nums: number[]): number {
let xor = 0;
for (const n of nums) {
xor ^= n; // pairs cancel: a ^ a = 0; identity: 0 ^ a = a
}
return xor; // only the unpaired element remains
}xor starts at 0, the XOR identity. It will absorb every element in one pass.a appears twice, xor ^ a ^ a = xor ^ 0 = xor. The lone element has no partner to cancel it.xor value is the single number. Return it.xor, regardless of input size.ones / twos state machine.a ^ b, isolate any differing bit (e.g. diff &= -diff), partition the array on that bit, and XOR each partition separately.nums[0] via the XOR loop (zero XOR-ed with one value is that value).function singleNumber(nums: number[]): number {
let xor = 0;
for (const n of nums) {
xor ^= n; // pairs cancel: a ^ a = 0; identity: 0 ^ a = a
}
return xor; // only the unpaired element remains
}xor, regardless of input size.Tally how many times each value appears, then return the one whose count is 1. The obvious approach before spotting the XOR trick.
function singleNumber(nums: number[]): number {
const counts = new Map<number, number>();
for (const n of nums) {
counts.set(n, (counts.get(n) ?? 0) + 1);
}
for (const [value, count] of counts) {
if (count === 1) return value;
}
return -1; // problem guarantees exactly one unique value
}n entries — O(n)space, violating the problem's constant-memory constraint. XOR-folding gets the same answer with a single accumulator and O(1) space.| "every element appears twice except one" | XOR fold (O(1) space) |
| "no extra memory" + find unique | bit manipulation / XOR |
| pairs that cancel, single survivor | a ^ a = 0; 0 ^ a = a |
| "appears k times" for k > 2 | bit count mod k (generalised XOR trick) |
function singleNumber(nums: number[]): number {
let xor = 0;
for (const n of nums) {
xor ^= n;
}
return xor;
}a ^ a = 0 (self-cancellation) and 0 ^ a = a (identity).[4, 1, 2, 1, 2] manually. What does the accumulator equal after processing the third element (2)?a ^ a = 0 guarantee correctness for this problem?singleNumber([7]) should return: