Day 10 of 90EasyLeetCode

Contains Duplicate

Return true if any value appears at least twice in the array, false if every value is distinct.

Pattern
Hash set membership
Time box
10 min
Time
O(n)
Space
O(n)

Try it first — give it 10 minutes

0/4 revealed

Stuck? Take one hint at a time. The struggle is where the pattern sticks.

    The problem in plain English

    Given an array nums, return true if some value shows up two or more times, and false if every value is unique.

    • [1, 2, 3, 1] → true (1 appears twice)
    • [1, 2, 3, 4] → false

    Start with brute force

    Compare every pair (i, j) with i < j. If any pair is equal, return true. That's O(n²) comparisons — too slow when n is 100,000.

    Building the intuition

    There are two classic ways to beat O(n²), and you should know both:

    1. Sort first. After sorting, equal values sit next to each other, so you only compare neighbours. O(n log n) time, and O(1) extra space if you sort in place.
    2. Remember what you've seen. Keep a hash set. For each number, ask "is it already in the set?" That's O(1) per number, so O(n) total.

    Interviewers love hearing both, because it shows you understand the time vs. space trade-off: sorting saves memory, the set saves time.

    The optimal approach

    1. Create an empty hash set seen.
    2. For each number x:
      • If x is in seen, return true — that's a duplicate.
      • Otherwise add x to seen.
    3. If the loop ends, every number was unique: return false.

    The code

    Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.

    class Solution:
        def containsDuplicate(self, nums: List[int]) -> bool:
            seen = set()
    
            for x in nums:
                if x in seen:      # already met this value -> duplicate
                    return True
                seen.add(x)
    
            return False           # every value was unique

    Dry run

    nums = [1, 2, 3, 1]

    xseen beforeIn set?Action
    1{}Noadd 1
    2{1}Noadd 2
    3{1, 2}Noadd 3
    1{1, 2, 3}Yesreturn true

    Complexity

    • Time: O(n). Each lookup and insert is O(1) on average.
    • Space: O(n). The set can hold every element if they're all unique.

    The sorting alternative is O(n log n) time, O(1) extra space (ignoring the sort's own stack).

    Edge cases and traps

    • One element. Can't have a duplicate: false.
    • Negative numbers and zero. A set handles them like any other value.
    • Returning early. Return true the moment you find a repeat — no need to finish the loop.
    • Python one-liner. len(set(nums)) != len(nums) is correct and fine to mention, but it always processes the whole array; the loop stops early.

    Say it in the interview

    "Brute force compares every pair, O(n²). I can sort and compare neighbours in O(n log n) with no extra memory, or use a hash set: for each number, if it's already in the set I return true, otherwise I add it. That's O(n) time and O(n) space. I'd go with the set unless memory is tight."

    Follow-ups interviewers ask

    • "Duplicates within distance k?" (Contains Duplicate II) Keep a map of value → last index, or a sliding-window set of size k.
    • "What if you can't use extra memory?" Sort in place and compare neighbours.
    • "What if the values are in a small range, say 0 to 1000?" Use a boolean array instead of a set — same idea, faster in practice.

    Practise the same idea

    This is Day 10 of a free 90-day plan 🐸

    90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.

    Get the full roadmap — free