Skip to main content

Contains Duplicate

nums is a list of integers Return true when some number shows up more than once Return false when each number shows up only once Example: [4, 9, 4] => true (4 shows up twice) Example: [5, 6, 7] => false (no repeats)

Contains Duplicate is a classic scan-and-record problem: the right bookkeeping beats brute force. It's a great warm-up that builds data-structure instincts without a ton of moving parts. Watch your edge cases around repeats, and be clear about what your map is storing.

Examples

You write

contains_duplicate(nums: List[int])

  1. Example 1

    Input
    nums = [1,2,3,1]
    returns
    true
  2. Example 2

    Input
    nums = [1,2,3,4]
    returns
    false

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Check for Duplicateseasy
2Part 2

Each locked part opens in the editor once the part before it passes.