Every number is a row of lamps, on or off. AND, OR and XOR work on every lamp at once, and x XOR x is zero, so pairs cancel.
▼Transcript
Welcome to the Bits and Math world. Here, every number is a row of lamps, each one on or off.
Each lamp is worth double the one on its right: one, two, four, eight. Thirteen is eight plus four plus one: on, on, off, on.
AND, OR and XOR compare two rows lamp by lamp, all at once. AND lights where both are on, OR where either is, XOR where they differ.
x XOR x is zero, and x XOR zero is x. So XOR four, one, two, one and two together. The pairs cancel, and four is left.
Two more tricks. x AND x minus one switches off the lowest lit lamp. A shift left by one doubles a number; a shift right halves it.
The clue: every number appears twice except one, count the one bits, a power of two, or add without plus or minus.
Each operation is one step on a fixed-width number, so one pass is linear time and constant space.
Now let's use it on Single Number.