Find duplicate entry in array of integers
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
Finding duplicates in an integer array is a classic problem, but the best solution depends on the constraints. If you only want a correct and readable answer, a hash set is usually the right tool. If memory is tight or the input has special structure, other techniques may be better.
That is why "find the duplicate" is not really one algorithmic question. It is a family of questions with different tradeoffs around time, space, and whether the input may be modified.
The Straightforward Hash-Set Solution
For general-purpose code, the simplest efficient approach is to track numbers already seen:
This solution is popular because it is:
- easy to understand
- '
O(n)average time' - correct for arbitrary integer values
The tradeoff is extra memory for the set.
Sorting When Extra Memory Must Stay Small
If modifying the array is allowed, sorting can reduce auxiliary memory:
This uses O(n log n) time because of the sort. It is slower than the hash-set approach on average, but sometimes that tradeoff is acceptable when memory pressure matters more.
Special Case: One Duplicate Under Range Constraints
Some interview-style versions of the problem come with strong guarantees:
- array length is
n + 1 - values are in the range
1throughn - exactly one value is duplicated, possibly more than once
That version can be solved with Floyd's cycle detection in O(1) extra space, but it is not a general duplicate-finding algorithm. It relies on the range constraints to interpret values as pointers.
So unless the problem statement includes those guarantees explicitly, do not force that technique onto a normal array problem.
Decide What "Duplicate" Means
You should also clarify the required output:
- return the first duplicate encountered
- return all duplicated values
- return every repeated occurrence
- return counts of each repeated value
These are different tasks. For example, if you need counts, a frequency map is more direct:
The algorithm you choose should match the exact output shape, not just the word "duplicate."
Mutating the Input Is a Real Tradeoff
Sorting is fine when a copied or reordered array is acceptable. It is a bad idea when:
- the original order matters
- the array is shared elsewhere
- mutation would surprise callers
In those cases, the hash-set solution is often the safer choice even if it uses extra space.
Common Pitfalls
- Choosing a specialized constant-space trick for a problem that does not satisfy its range constraints.
- Forgetting to define whether the goal is one duplicate, all duplicates, or duplicate counts.
- Sorting in place without realizing the original order was important.
- Adding the same duplicate multiple times instead of deduplicating the output.
- Over-optimizing an easy
O(n)hash-set problem into something harder to read without a real need.
Summary
- For the general case, a hash set is usually the clearest efficient solution.
- Sorting is a good alternative when modifying or copying the array is acceptable.
- Constant-space tricks only apply to special constrained versions of the problem.
- Clarify whether you need one duplicate, all duplicates, or counts.
- Let the input constraints drive the algorithm choice instead of forcing one favorite technique everywhere.
Related reading
- Find duplicate in array with a memory efficient approach
- Find duplicates in an array, without using any extra space
- Find duplicates in an unsorted sequence efficiently
- Find earliest time for k empty group
- Find elements in one list that are not in the other
- Find existence of number in a sorted list in constant time? Interview question
- Find first element by predicate
- Find first element in a sequence that matches a predicate

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.