Find duplicate element in array in time On
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 a duplicate in linear time is usually straightforward if you are allowed extra memory. The standard solution is to scan the array once while tracking values you have already seen in a hash set. That gives average O(n) time and is the most practical answer for general input.
The General O(n) Solution
The idea is simple:
- create an empty set
- walk through the array once
- if the current value is already in the set, you found a duplicate
- otherwise add it and continue
Python example:
This runs in linear time on average because set membership and insertion are average O(1) operations.
Returning All Duplicates
Sometimes the problem is not "find one duplicate" but "list all values that appear more than once". In that case, keep two sets:
- '
seenfor values encountered at least once' - '
duplicatesfor values confirmed to repeat'
This is still O(n) time, with O(n) extra space in the worst case.
Count Map Version
If you also need frequencies, use a hash map instead of a set:
This is useful when the consumer needs more than a yes-or-no answer.
Can You Do It in O(n) Time and O(1) Extra Space
Sometimes interview versions add stronger constraints, such as:
- array length is
n + 1 - values are in the range
1throughn - exactly one value is duplicated
Under those assumptions, there are specialized techniques. One famous example is Floyd's cycle detection, which treats the array like a linked structure and finds the repeated value in linear time with constant extra space.
This is elegant, but it only works because the input obeys a very specific structure. For arbitrary arrays, the hash-set solution is the correct default.
Why Sorting Is Usually Not the Best Answer
Another common idea is to sort the array and then scan neighboring elements. That works, but it changes the time complexity:
- sorting usually costs
O(n log n) - the scan after sorting is
O(n)
So the total time is typically O(n log n), not O(n). It may still be acceptable in practice, but it does not satisfy the strict linear-time requirement.
Choosing the Right Approach
Use the hash-set solution when:
- input is arbitrary
- you want a simple implementation
- '
O(n)extra memory is acceptable'
Use a specialized constant-space technique only when the input guarantees are strong enough to support it. Otherwise, "clever" constant-space tricks often become wrong or fragile.
Common Pitfalls
The most common mistake is claiming an O(n log n) sorting solution is O(n) because the final scan is linear. The sort still dominates.
Another issue is ignoring input constraints. Floyd's cycle detection is excellent for the classic repeated-number problem, but it does not solve the general "find duplicates in any array" task. Developers also sometimes forget that hash-based O(1) operations are average-case assumptions, not strict worst-case guarantees.
Summary
- For general arrays, the standard linear-time solution uses a hash set.
- A set finds the first duplicate in average
O(n)time withO(n)extra space. - A map is useful when you need duplicate counts instead of only detection.
- Constant-space linear-time tricks require special input constraints.
- Sorting is often fine, but it is usually
O(n log n), not trueO(n).
Related reading
- Find duplicate entry in array of integers
- 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 if two arrays contain the same set of integers without extra space and faster than NlogN

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.