LeetCode 136 Single Number - Solution

The core idea

Use XOR properties:

  • aa=0a \oplus a = 0 (anything XORed with itself is 0).
  • a0=aa \oplus 0 = a (anything XORed with 0 is itself).
  • XOR is commutative and associative.

XOR all numbers together. Pairs cancel out (become 0). The single number remains.