Explain the XOR trick.
Skip to solutionKEEP THE
easyDSA
How do you find the single number where every other element appears twice?
331 views
01
Understand the problem
arraysbit-manipulation
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
XOR all elements together. Pairs cancel (x ^ x = 0) and x ^ 0 = x, so the lone element remains. O(n) time, O(1) space — no hash set needed. Variants with triples need bit-count tricks.
Solution ready — 2 min read
Classified // press E to declassify
04
Read the code
XOR fold
Run Playgrounddef single_number(nums):
result = 0
for x in nums:
result ^= x
return result
# --- demo --- (one-liner: reduce(xor, nums))
print(single_number([4, 1, 2, 1, 2])) # 405
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 21 of 127 decoded in the Data Structures & Algorithms track. One more won't hurt.