algorithms
computer science
data structures
list processing
coding tutorial

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

  1. Initialize a variable n as the length of the list.
  2. Use a for-loop to iterate through the list using index i.
  3. For each element list[i], use another for-loop with index j (starting from i+1) to traverse subsequent elements.
  4. If list[i] == list[j], return list[i].

Time Complexity

The time complexity of this approach is O(n2)O(n^2), 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 O(1)O(1) time complexity.

Algorithm

  1. Initialize an empty hash set seen.
  2. Iterate through the list:
    • For each element, check if it exists in the seen set.
    • If it does, return that element.
    • Otherwise, add the element to the seen set.

Example

python
1def find_repeated_number(lst):
2    seen = set()
3    for num in lst:
4        if num in seen:
5            return num
6        seen.add(num)

Time Complexity

The time complexity is O(n)O(n), 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

  1. Phase 1 (Tortoise and Hare Step):
    • Initialize tortoise and hare variables at the start of the list.
    • Move tortoise at normal speed (lst[tortoise]) and hare at twice the speed (lst[lst[hare]]).
    • Stop when they meet; this confirms a cycle due to repetition.
  2. Phase 2 (Finding the Entrance to Cycle):
    • Reset tortoise to the start of the list.
    • Move both tortoise and hare at normal speed.
    • The point at which they meet is the repeated number.

Time Complexity

Both phases run in O(n)O(n) time, making this method efficient for large datasets.

Summary

Here's a quick summary of the approaches we've discussed:

MethodTime ComplexitySpace ComplexityUse Case
Naive Nested LoopsO(n2)O(n^2)O(1)O(1)Small lists
Hash SetO(n)O(n)O(n)O(n)General-purpose
Floyd's Cycle DetectionO(n)O(n)O(1)O(1)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.


Course illustration
Course illustration

All Rights Reserved.