beginner · hashing · sets · arrays

Has any badge been scanned twice?

Problem

A door logs badge numbers as people enter. Decide whether any badge number appears more than once in the log.

Constraints

  • 0 ≤ n ≤ 32 (visualization limit)
  • badge numbers are integers from −999 to 999

Examples

[41, 7, 19, 7] → true

[3, 1, 2] → false

Show the solution

Hash set of seen badges

Keep every badge seen so far in a set; the first badge already in the set is a duplicate.

Why it is correct

The set always holds exactly the badges before the current position, so the check succeeds exactly at the second occurrence of the first repeated badge; if it never succeeds, all badges are distinct.

Complexity

Time O(n) expected, extra space O(n). One membership test and at most one insert per badge, each expected O(1).

For contrast: Compare every pair, or sort and compare neighbours. That costs O(n²) or O(n log n) time and O(1) or O(n) for the sorted copy space.

Edge cases

  • Empty or one-badge log: false.
  • The duplicate may be the last element: the whole log is scanned.

Common mistakes

  • Using a list for "seen": membership becomes O(n) and the whole thing O(n²).

Step through it

The solution in four languages

Each is checked against one shared set of cases and a randomized comparison with a brute-force reference.

namespace Bekadan.Dsa.Examples.Problems;
 
public static class DuplicateDetection
{
    /// <summary>Whether any value appears twice. Expected O(n) time, O(n) space.</summary>
    public static bool HasDuplicate(int[] badges)
    {
        var seen = new HashSet<int>();
        foreach (int badge in badges)
        {
            if (seen.Contains(badge))
                return true;
            seen.Add(badge);
        }
        return false;
    }
}

Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Problems/DuplicateDetection.cs

from collections.abc import Iterable
 
 
def has_duplicate(badges: Iterable[int]) -> bool:
    """Whether any value appears twice. Expected O(n) time, O(n) space."""
    seen: set[int] = set()
    for badge in badges:
        if badge in seen:
            return True
        seen.add(badge)
    return False

Tested on CPython 3.14.0 · examples/python/dsa_examples/problems/duplicate_detection.py

package dev.bekadan.dsa.problems;
 
import java.util.HashSet;
import java.util.Set;
 
public final class DuplicateDetection {
    private DuplicateDetection() {
    }
 
    /** Whether any value appears twice. Expected O(n) time, O(n) space. */
    public static boolean hasDuplicate(int[] badges) {
        Set<Integer> seen = new HashSet<>();
        for (int badge : badges) {
            if (seen.contains(badge)) {
                return true;
            }
            seen.add(badge);
        }
        return false;
    }
}

Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/problems/DuplicateDetection.java

#pragma once
 
#include <span>
#include <unordered_set>
 
namespace bekadan::dsa::problems {
 
// Whether any value appears twice. Expected O(n) time, O(n) space.
inline bool has_duplicate(std::span<const int> badges) {
    std::unordered_set<int> seen;
    for (int badge : badges) {
        if (seen.contains(badge))
            return true;
        seen.insert(badge);
    }
    return false;
}
 
}  // namespace bekadan::dsa::problems

Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/problems/duplicate_detection.hpp