1. XOR identity
Zero is the identity: x XOR 0 = x.
let result = 0Typorithm · easy
Step 1 of 8 · intro card
Bit Manipulation
XOR every element; equal pairs cancel to zero, so the one unpaired value remains.
XOR is its own inverse: x XOR x = 0, and x XOR 0 = x.
Folding the whole array with XOR cancels every value that appears twice.
Only the element appearing once is left, found in one pass with O(1) memory.
x XOR x = 0, x XOR 0 = x.
Pairs cancel out.
One pass, O(1) space.
function singleNumber(nums) {
let result = 0;
for (const num of nums) {
result ^= num;
}
return result;
}Connect each part of the code to the concept before you type.
Zero is the identity: x XOR 0 = x.
let result = 0XOR every value; pairs cancel to zero, leaving the unique one.
result ^= numAfter all cancellations, only the single number survives.
return resultRead the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.
Algorithm lab · interactive
InputNumbers 4,1,2,1,2
Follow temporary values, local state and supporting structures at this step.
No temporary values have been saved in this frame yet.
Around this step
Small, reproducible examples · ← → step · Space play/pause · Home/End first/last state