DEV Community

Andrei Iatsuk
Andrei Iatsuk

Posted on

3 2

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)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay