DocsHub
Animations

Remove Duplicates — Visual Walkthrough

Watch the seen set grow as duplicates get rejected and unique values are collected into the result.

Remove Duplicates

Input: [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 9]

For each number — check if it is already in the seen set. If not, add it to both result and seen. If yes, skip it.

Remove Duplicatesset tracking · O(n)
input11 items

Input array

3
1
4
1
5
9
2
6
5
3
9
seen set0 items
empty
result [ ]0 items
empty
remove_duplicates.py
def remove_duplicates(items):
seen = set()
result = []
for item in items:
# check current item
if item not in seen:
result.append(item); seen.add(item)
# else: skip duplicate
return result
Call remove_duplicates([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 9])
step 0/37
0%

How it works

def remove_duplicates(items):
    seen = set()
    result = []

    for item in items:
        if item not in seen:
            result.append(item)
            seen.add(item)

    return result

The seen set does the heavy lifting. Checking item in set is O(1) — instant regardless of how many items are already in it.

Using a set for seen is critical. Checking item in list gets slower as the list grows — O(n) per check. Checking item in set is always O(1). For 10,000 items that is a 10,000x difference.

Complexity

TimeSpace
With setO(n)O(n)
Without set (list check)O(n²)O(n)

On this page