Finding out the duplicate element in an array
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
Duplicate detection in arrays appears in interviews, data cleaning pipelines, fraud checks, and import validation jobs. There is no single best algorithm because constraints differ across use cases. The right approach depends on whether you need one duplicate or all duplicates, whether input can be modified, and how much memory you can spend.
Clarify the Problem Contract First
Before coding, define expected behavior:
- Should output include each duplicate once or include repeated occurrences.
- Can the original array be mutated.
- Are values bounded, such as range
1throughn. - Is data streamed or fully in memory.
Those decisions determine viable algorithms and prevent incorrect optimization.
Set-Based Linear Scan
For most practical code, a set-based scan is simplest and fast.
Complexity:
- Time:
O(n)average. - Extra memory:
O(n).
This is usually the best default when memory is acceptable.
Frequency Map When Counts Matter
If you need duplicate counts, use a dictionary or Counter.
This gives both which values are duplicated and how many times each appears.
Sorting-Based Method for Lower Extra Memory
If mutating input is allowed and extra memory is limited, sort then compare neighbors.
Complexity:
- Time:
O(n log n). - Extra memory: low, depending on sorting implementation.
Special Case: Exactly One Duplicate in Range
Some interview problems guarantee values from 1 through n with one duplicated value. In that specific case, Floyd cycle detection gives constant extra space.
Do not use this method for arbitrary arrays. It depends on strict constraints.
Large-Scale and Streaming Data
For very large datasets, full in-memory sets may be too expensive.
Practical options:
- Chunk processing with on-disk key-value storage.
- Database grouping with indexed columns.
- Probabilistic prefiltering with Bloom filters.
Bloom filters help detect likely duplicates early, but they can produce false positives, so you still need exact confirmation logic.
Choosing an Approach in Real Projects
A simple decision guide:
- Need exact duplicates quickly and memory is acceptable: set-based scan.
- Need counts for reporting: frequency map.
- Need lower auxiliary memory and can mutate: sort then scan.
- Strict one-duplicate constrained problem: Floyd method.
- Data too large for memory: chunking or external storage.
Pick the approach that matches your data contract, not the one that looks most clever.
Common Pitfalls
- Using a specialized algorithm without verifying input constraints. Fix: validate assumptions before method selection.
- Returning the same duplicate multiple times. Fix: track duplicates in a dedicated set or check last appended value.
- Mutating arrays unexpectedly with in-place sort. Fix: document mutation or sort a copied array.
- Ignoring non-integer or null values in mixed datasets. Fix: add input validation and explicit type handling.
- Optimizing for asymptotic complexity while ignoring memory limits. Fix: evaluate both time and space for production-size inputs.
Summary
- Duplicate detection has multiple correct solutions based on constraints.
- Set-based scans are the best default for exact linear detection.
- Frequency maps are ideal when counts are required.
- Sorting trades extra memory for higher time complexity.
- Specialized constant-space methods only work under strict problem contracts.
Related reading
- Finding out the minimum difference between elements in an array
- Finding out whether there exist two identical substrings one next to another
- Finding pairs with product greater than sum
- Finding positions of milestones given their pairwise distances
- Finding overlapping data in arrays
- Finding patterns in list
- Finding reachable vertices for every vertex in a directed graph
- Finding set of pairs that correspond to list of sums

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.