Hacker News new | past | comments | ask | show | jobs | submit login

Iterate through the list bitwise-XORing the numbers together into an accumulator. Because x ^ x = 0, and x ^ 0 = x, and ^ is commutative (x ^ y = y ^ x), the final value in the accumulator for a list like [x, y, y, x, z] is:

    x ^ y ^ y ^ x ^ z 
  = x ^ x ^ y ^ y ^ z
  = 0 ^ 0 ^ z
  = z



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: