find the only unpaired element in the array
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When every value in an array appears exactly twice except one, the cleanest solution is not sorting or counting with a map. It is a bitwise XOR pass, which finds the unpaired element in linear time and constant extra space.
Why XOR Solves the Problem
XOR has two properties that make this work:
- '
a ^ aequals0' - '
a ^ 0equalsa'
Because XOR is also associative and commutative, pairs cancel no matter where they appear in the array. After processing every element, only the unique value remains.
For the array 4, 3, 2, 4, 1, 3, 2, the cancellation looks like this conceptually:
- '
4 ^ 4becomes0' - '
3 ^ 3becomes0' - '
2 ^ 2becomes0' - '
0 ^ 0 ^ 0 ^ 1leaves1'
That gives you the answer in one pass.
The Best General Solution for This Exact Problem
Here is a small Python implementation:
This runs in O(n) time and O(1) extra space.
That is hard to beat because every element must be inspected at least once, so O(n) is already optimal in time. The XOR solution also avoids allocating a dictionary or sorting the array.
When a Hash Map Is Still Useful
The XOR trick only works under very specific assumptions:
- every paired value appears exactly twice
- exactly one value appears once
- values are integers or at least types where XOR makes sense
If the rules are more general, a counting structure is safer.
This still runs in linear time, but it uses extra memory proportional to the number of distinct values.
Sorting Is Usually Not the Best First Choice
Another valid approach is to sort the array and scan it in pairs. That works well enough for interviews or when the array is already sorted, but the runtime becomes O(n log n) because of the sort.
That is fine if you also need the sorted order for some other reason. If the only goal is the unpaired element, XOR is more direct.
How to Explain the XOR Method Clearly
People often know the code but struggle to explain why it works. The clean explanation is:
- every paired value cancels itself under XOR
- order does not matter
- zero disappears under XOR with the remaining value
That makes the proof small and complete.
Edge Cases to Think About
The XOR method handles negative integers as well because it operates on bit patterns, not on numeric sign semantics in a special way. It also works regardless of where the unpaired element appears.
What it does not do is validate the input contract. If the array contains two different unpaired values, the result becomes the XOR of those values, not a meaningful answer to the original problem.
So in production code, decide whether the input guarantee is trusted. If not, add validation or use a counting approach.
Common Pitfalls
The biggest pitfall is applying XOR to a problem that does not meet the “all others appear exactly twice” rule. In that case, the result is not reliable.
Another mistake is choosing a hash map first out of habit. A counting solution works, but it is more memory-heavy than needed for this exact problem.
A third issue is forgetting that sorting changes the time complexity. It may simplify reasoning, but it is not the most efficient route here.
Summary
- XOR is the best fit when every element appears twice except one.
- The method runs in
O(n)time andO(1)extra space. - It works because equal values cancel under XOR.
- Use a hash map when the occurrence pattern is more general.
- Use sorting only when sorted order is also useful for the broader task.

