Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
XOR every element together; pairs cancel to 0 and the result is the odd-occurring number. O(n) time, O(1) space.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you know the XOR-cancellation trick for O(1) space.
Where candidates slip
Reaching for a frequency map instead of XOR.
How to stand out
State the x^x=0 / x^0=x properties that make it work.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
XOR accumulation
1Hint 1Explore hintHide hint
What property of XOR makes pairs disappear?
2Hint 2Explore hintHide hint
Fold all elements with XOR — what survives?
3Hint 3Explore hintHide hint
Can you do it without a hash map (O(1) space)?
A · Core idea
Every value appears an even number of times except one. XOR all elements together: even occurrences cancel to 0, leaving the odd-count value. O(n) time, O(1) space.
Why XOR cancels even occurrences. XOR has two key properties: x ^ x = 0 (a value XOR'd with itself vanishes) and x ^ 0 = x. A value appearing an even number of times pairs up and cancels to 0; the value appearing an odd number of times has one un-cancelled copy left. XOR is also commutative/associative, so order doesn't matter.
Why XOR beats a hash map. A frequency map also works but costs O(n) extra space. The XOR fold needs a single accumulator — O(1) space — which is the answer the interviewer is after.
Seed at 0. Start the accumulator at 0 (the XOR identity) so the first element folds in cleanly.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [1, 2, 1, 2, 3]1^2^1^2
the two 1s and 2s cancel
03C · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
This O(1)-space XOR trick is the same one behind 'Single Number' — recognising it is the whole point.
ComplexityO(n) time, O(1) space — one XOR per element with a single accumulator — O(n) time, O(1) space.
E · Edge cases and pitfalls4nums = [4, 4, 7] — 4s cancel → 7.Explore edge casesHide edge cases
Edge cases handled
nums = [4, 4, 7]4s cancel → 7.nums = [9]Single element → 9.
Pitfalls
- Using a hash map (O(n) space) when XOR is O(1).
- Initialising the accumulator to something other than 0.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int oddOccurring(int[] nums) { int x = 0; for (int v : nums) x ^= v; return x; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Using a hash map (O(n) space) when XOR is O(1).
- Initialising the accumulator to something other than 0.
Revise
Carry the pattern into the interview
Pattern to remember
XOR cancellation
Code pattern to memorize
x = 0; for v: x ^= v; return xRules to remember
- 1x ^ x = 0.
- 2x ^ 0 = x.
- 3XOR all → odd one.
Use this pattern when
- Odd-count / unpaired element
- Pairing cancellation
Interview takeaway
XOR all elements to isolate the odd-count value in O(1) space.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: XOR cancellation. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Single NumberThe 'appears twice except one' twin.
- Single Number IIIComing soonTwo odd-count values — partition by a differing bit.