Finding a number that repeats even no of times where all the other numbers repeat odd no of times
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
This problem looks similar to the classic XOR puzzle, but the parity pattern is reversed. If exactly one number appears an even number of times and every other distinct number appears an odd number of times, plain XOR does not directly reveal the answer, so the safest general approach is frequency counting.
Why XOR Alone Does Not Solve This Version
For the well-known problem where one value appears an odd number of times and all others appear an even number of times, XOR works because the even counts cancel out.
Here the situation is different:
- the target value appears an even number of times
- every other value appears an odd number of times
If you XOR the whole array, the even-repeating number disappears and the odd-repeating numbers remain mixed together.
For example, in:
[2, 2, 3, 3, 3, 5, 5, 5]
the answer is 2, but the XOR of the full array is:
2 ^ 2 ^ 3 ^ 3 ^ 3 ^ 5 ^ 5 ^ 5 = 3 ^ 5
That does not isolate the even-repeating number. So the first important insight is that this is not the standard XOR problem.
Use Frequency Counting
The straightforward and correct general solution is to count how many times each value appears, then return the one whose count is even.
This runs in linear time and is easy to reason about. It also matches the real structure of the problem instead of forcing a bit trick where it does not fit.
Complexity and Data Structure Choices
Using a hash map or Counter gives:
- '
O(n)time' - '
O(m)extra space, wheremis the number of distinct values'
That is usually the right tradeoff for an unsorted array.
If the value range is known to be small and nonnegative, a counting array can also work:
This is still the same idea as frequency counting. The only difference is the storage structure.
Be Careful About the Problem Assumptions
The problem is well-defined only if the input guarantees exactly one distinct value with even frequency. If the input can contain:
- more than one even-frequency value
- no even-frequency value
- negative numbers when you planned to use an index-based counting array
then the implementation needs extra validation or a more general data structure.
That is another reason the hash-map solution is a good default. It makes fewer assumptions about the input domain.
When a Smarter Trick Might Exist
Extra constraints can change the best answer. For example, if the array is sorted, you might exploit runs of equal values. If the value range is extremely small, a compact counting structure may be more efficient.
But for the general unsorted problem statement, frequency counting is the correct baseline. It is robust, linear-time, and conceptually honest about what information the input parity pattern actually gives you.
Common Pitfalls
- Applying the usual XOR trick without noticing that this problem has the opposite parity pattern.
- Assuming the input always contains exactly one even-frequency value without checking the real constraints.
- Choosing an array-based counting method when values can be negative or very large.
- Overcomplicating the problem when a simple frequency map already gives linear time.
- Confusing "appears an even number of times" with "is an even number."
Summary
- This is not the standard XOR parity problem.
- XOR cancels the even-repeating value instead of isolating it.
- The safest general solution is to count frequencies and return the value with an even count.
- A hash map gives linear time and works well for unsorted input.
- Only switch to more specialized tricks when the problem gives extra structure beyond the basic statement.
Related reading
- Finding a prime number after a given number
- Finding a single number in a list
- finding abc... mod m
- Finding Aii in a sorted array with duplicates
- Finding All Combinations Cartesian product of JavaScript array values
- Finding all cycles in a directed graph
- Finding a ray that intersects a polygon as many times as possible
- Finding a stable placement of an irregular non-convex shape

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.