Skip to content
Preptima
easyCodingConceptEntryMidSenior

Every value in an array appears twice except one. Find it, and tell me when bit tricks are worth reaching for

5 min readUpdated 2026-08-15

Answer

Short answer

XOR-folding the array leaves the unpaired value because a^a is 0 and XOR is commutative and associative, giving O(n) time and O(1) space under a precondition worth stating.

Keep bit manipulation explicit in the answer because that is the concept the interviewer is actually trying to test.

Was this answer helpful?

Likely follow-ups

  • Now every value appears three times except one. What changes, and what is the cost?
  • How do you enumerate every submask of a mask, and what is the total work across all masks?
  • Why is `1 << 32` equal to 1 in Java rather than 0?
  • Where have you used a bitmask in production code, and what did it replace?

Related questions

Further reading

Related Topics