XOR Properties

XOR is self-inverse and associative.

Key properties: 1.1. aa=0a \oplus a = 0 (self-inverse)

2.2. a0=aa \oplus 0 = a (identity)

3.3. Commutative: ab=baa \oplus b = b \oplus a

4.4. Associative: (ab)c=a(bc)(a \oplus b) \oplus c = a \oplus (b \oplus c)

Application: Finding the unique element.

If all elements appear twice except one, XOR them all. Pairs cancel out, leaving the unique element.

result = 0
for num in nums:
    result = result ^ num
return result

Why it works: aa=0a \oplus a = 0, so pairs vanish. Only the unique element remains.

Time: O(n)O(n).

Space: O(1)O(1) (excluding input array).