Skip to content
StackDecode
Bit manipulationAnimated

Find Single Non-Repeating Element

XOR every number; pairs cancel and the single one remains.

Time

O(n)

Extra space

O(1)

Use it for

One unique value among pairs.

How it works

  1. Every number appears twice except one.
  2. XOR all the numbers together.
  3. Equal numbers cancel (x ^ x = 0) and 0 changes nothing, so only the single number survives.
  4. One pass, O(1) extra memory.

Watch it run

Loading animation…