Algorithm to find a repeated number in a list that may contain any number of repeats
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Finding duplicate numbers in a list is a common problem in computer science and software engineering. This issue often arises in various domains such as data validation, memory optimization, and database management. In this article, we will explore different algorithms to efficiently find a repeated number in a list, even when it may contain multiple repeats. We will explain the technical mechanisms underlying these algorithms and provide examples for better understanding.
Problem Statement
Given a list of numbers, identify at least one repeated number, regardless of how many times it appears in the list. For the sake of simplicity, we assume that the list is non-empty and contains integer values.
Naive Approach
The simplest way to find a repeated number is to use two nested loops. The first loop iterates over each element, while the second loop checks the rest of the list to see if the current element has a duplicate.
Algorithm
- Initialize a variable
nas the length of the list. - Use a for-loop to iterate through the list using index
i. - For each element
list[i], use another for-loop with indexj(starting fromi+1) to traverse subsequent elements. - If
list[i] == list[j], returnlist[i].
Time Complexity
The time complexity of this approach is , which makes it inefficient for large lists.
Hash Set Approach
A more efficient algorithm involves using a hash set to track elements we've seen so far. This method leverages the fact that lookups in a hash set generally have time complexity.
Algorithm
- Initialize an empty hash set
seen. - Iterate through the list:
- For each element, check if it exists in the
seenset. - If it does, return that element.
- Otherwise, add the element to the
seenset.
Example
Time Complexity
The time complexity is , as each element is processed at most once.
Floyd's Cycle Detection (Tortoise and Hare)
A creative method originally designed for detecting cycles in linked lists can also be used to identify duplicates in a list that represents a permutation of integers starting from 1.
Algorithm
- Phase 1 (Tortoise and Hare Step):
- Initialize
tortoiseandharevariables at the start of the list. - Move
tortoiseat normal speed (lst[tortoise]) andhareat twice the speed (lst[lst[hare]]). - Stop when they meet; this confirms a cycle due to repetition.
- Phase 2 (Finding the Entrance to Cycle):
- Reset
tortoiseto the start of the list. - Move both
tortoiseandhareat normal speed. - The point at which they meet is the repeated number.
Time Complexity
Both phases run in time, making this method efficient for large datasets.
Summary
Here's a quick summary of the approaches we've discussed:
| Method | Time Complexity | Space Complexity | Use Case |
| Naive Nested Loops | Small lists | ||
| Hash Set | General-purpose | ||
| Floyd's Cycle Detection | Special cases (permuted integers) |
Conclusion
Finding repeated numbers in a list is a fundamental problem with applications across numerous fields. We've reviewed several approaches, from naive nested loops to more sophisticated algorithms like Floyd's Cycle Detection. Choosing the right algorithm depends largely on the specific constraints and requirements of your application. For general use, hash sets are effective, while specialized scenarios might benefit from cycle detection techniques.

