DEV Community

Andrey
Andrey

Posted on

Finding unpaired number

Image description

The explanation lies in the binary representation of the number. It uses the same trick as the bitwise encryption. When we meet the same character twice, the XOR operation gives 0. So all that is left is a number without a pair.

Example
[1, 2, 1]
0 ^ 1 (000 ^ 001) = 1 (001)
1 ^ 2 (001 ^ 010) = 3 (011)
3 ^ 1 (011 ^ 001) = 2 (010)
Answer is 2

Top comments (0)