LeetCode 217 · Easy

Contains Duplicate Explained (LeetCode 217)

Updated Nov 27, 2026 · 3 min read

Walk the list once and keep every number you have seen in a set. Before adding a number, check whether it is already in the set; if it is, return True right away. If the loop finishes, every number was different, so return False. That is O(n) expected time and O(n) extra space.

Watch onYouTubeInstagram

Key points

  • ●Comparing every pair of 100,000 numbers is about five billion checks: O(n²).
  • ●A set answers "have I seen this before?" in expected constant time.
  • ●Check first, then add. Return True on the first repeat.
  • ●One pass: O(n) expected time, O(n) extra space for the set.
Step-by-step visual sheet: Contains Duplicate
The full visual sheet, made with Edithly.Open the interactive sheet

The riddle: did anyone use a ticket twice?

Raj checks tickets at the door. Tonight's tickets come in as 4, 7, 2, 7, 9. Did anyone get in twice on the same ticket?

You can spot it by eye: 7 shows up twice. That is LeetCode 217, Contains Duplicate. You get a list of whole numbers and return True if any number appears at least twice, or False if every number is different. The list can hold up to 100,000 numbers, and that limit is what makes the easy-looking problem interesting.

The slow way: compare every pair

The obvious approach compares each ticket with every ticket after it.

for i in range(len(nums)):
    for j in range(i + 1, len(nums)):
        if nums[i] == nums[j]:
            return True
return False

Five tickets means 10 comparisons. In general there are about n(n-1)/2 pairs, so 100,000 tickets means roughly five billion comparisons. It never misses a duplicate and it uses no extra memory, but it's O(n²) time, and that times out.

The idea: remember what you've seen

Raj doesn't need to compare tickets with each other. He just keeps a list of every ticket he has already let in. For each new ticket he asks one question: is it on the list?

  • Not on the list? Add it and move on.
  • Already on the list? That's the duplicate. Stop.

In Python that list is a set. Checking whether a value is in a set, and adding one, are expected constant-time operations. So instead of comparing each ticket with all the others, you do one quick lookup per ticket.

Trace it on Raj's tickets

Ticket In seen? seen after
4 no {4}
7 no {4, 7}
2 no {2, 4, 7}
7 yes stop, return True

Ticket 9 never gets checked, because the answer is already known. Step 5 of the visual sheet above traces this same run, plus a second example, [5, 1, 8], where every ticket is new, the set grows to {1, 5, 8}, and the function returns False.

The code in Python

def contains_duplicate(nums):
    seen = set()
    for number in nums:
        if number in seen:
            return True
        seen.add(number)
    return False

The order inside the loop matters: check first, then add. The final return False handles the case where the whole list was scanned without a repeat.

There's also a one-line version, return len(set(nums)) != len(nums). It's correct and also O(n), but it always builds the full set, while the loop can stop early on the first repeat.

Complexity

  • Time: O(n) expected, one pass with constant-time set checks.
  • Space: O(n) extra, since the set can hold every number.
  • Brute force, for comparison: O(n²) time, O(1) space.

This is the classic trade: spend memory to save time. The set costs space, but it saves you from comparing the same old values over and over.

The trap to watch for

Adding before checking. If you write seen.add(number) first and then test if number in seen, every number is "already seen" and the function returns True on the first ticket. Check, then add.

Why does this matter? With the wrong order, [1, 2, 3] comes back True even though nothing repeats, and a quick test on an all-distinct list is the fastest way to catch it.

The other trap is overthinking it. You don't need to count how many times each number appears. One repeat is enough to answer True, so return the moment you find it.

Frequently asked questions

What is the fastest way to check for duplicates in a Python list?

Scan the list once with a set. For each number, return True if it is already in the set, otherwise add it. Set lookups and inserts are expected constant time, so the whole scan is O(n).

Why not just compare every pair?

It works, but n numbers have about n(n-1)/2 pairs. For 100,000 numbers that is roughly five billion comparisons, which is O(n²) and too slow for the problem's limits.

What is the time and space complexity of the set solution?

O(n) expected time, because each number is checked and added once, and O(n) extra space, because the set can hold one entry per number when all of them are different.

Can I use len(set(nums)) != len(nums)?

Yes, that one line gives the same answer in O(n) time and space. The loop version has one advantage: it stops at the first repeat instead of building the whole set.

Turn any problem or document into a visual sheet like this one

Paste a LeetCode problem, a PDF or a link into Edithly and get a step-by-step sheet you can study, share or quiz yourself on.

Make this sheet in Edithly, free