◀ THE GRIND — ARRAYS & HASHING

Contains Duplicate

The drill: Decide whether any value appears more than once in the array — a single repeated value anywhere is enough to say yes.

THE BRIEFING — THE FULL DRILL, IN MY OWN WORDS

A list of integers arrives, and the only question is whether any single value shows up more than once anywhere inside it.

There's no need to say which value repeats or how many times — a plain yes-or-no answer is all that's asked for, based purely on whether at least one collision exists.

Order in the array carries no meaning here; only presence and repetition matter. A completely unique list, even one arranged in a wildly scrambled order, answers no.

EX 01
nums = [3, 8, 3]
true
REPEAT STRADDLING THE MIDDLE
EX 02
nums = [1, 2, 3, 4]
false
EX 03
nums = [7]
false
SINGLE ELEMENT
THE HINTS — TAKE ONLY WHAT YOU NEED
HINT 1 THE NUDGE

The quadratic way compares every pair. But “is there a repeat” never needs pairs — it needs memory of what you have already walked past.

HINT 2 THE STRUCTURE

A set answers “have I seen this?” in O(1). What is the earliest moment you can answer yes?

HINT 3 ONE STEP FROM THE ANSWER

Insert as you scan: if the value is already in the set, you are done. Finishing the scan clean means no duplicates.

COACH'S BOARD — THE PATTERN, STEP BY STEP
THE SEEN SETPATTERN · HASH SETnums = [3, 8, 3]
3
8
3
SEEN SET
— empty —
STEP 1

The seen set starts empty. Watch for the moment a value repeats one already stored.

STEP 1 / 7 · ← → WORK TOO
THE SPLITS — TWO PACES, TWO LANGUAGES
grind/contains-duplicate.pyRACE PACE
LANG ▸
PACE ▸
class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        seen = set()
        for v in nums:
            if v in seen:
                return True
            seen.add(v)
        return False
TIME O(N)SPACE O(N)PYTHON · RACE PACE · 8 LN

✓ CHIP-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED