0
0
PythonProgramBeginner · 2 min read

Python Program to Find Duplicate in List

You can find duplicates in a list in Python by using a set to track seen items and collecting duplicates like this: duplicates = set(x for x in lst if lst.count(x) > 1).
📋

Examples

Input[1, 2, 3, 4]
Outputset()
Input[1, 2, 2, 3, 4, 4, 4]
Output{2, 4}
Input[]
Outputset()
🧠

How to Think About It

To find duplicates, look at each item in the list and check if it appears more than once. Keep track of items you have already seen to avoid repeating duplicates. Collect these repeated items in a separate group.
📐

Algorithm

1
Get the input list.
2
Create an empty set to store duplicates.
3
For each item in the list, check if it appears more than once.
4
If yes, add it to the duplicates set.
5
Return the set of duplicates.
💻

Code

python
def find_duplicates(lst):
    duplicates = set()
    seen = set()
    for item in lst:
        if item in seen:
            duplicates.add(item)
        else:
            seen.add(item)
    return duplicates

# Example usage
my_list = [1, 2, 2, 3, 4, 4, 4]
print(find_duplicates(my_list))
Output
{2, 4}
🔍

Dry Run

Let's trace the list [1, 2, 2, 3, 4, 4, 4] through the code.

1

Start with empty sets

duplicates = set(), seen = set()

2

Check item 1

1 not in seen, add 1 to seen -> seen = {1}

3

Check item 2

2 not in seen, add 2 to seen -> seen = {1, 2}

4

Check next item 2

2 in seen, add 2 to duplicates -> duplicates = {2}

5

Check item 3

3 not in seen, add 3 to seen -> seen = {1, 2, 3}

6

Check item 4

4 not in seen, add 4 to seen -> seen = {1, 2, 3, 4}

7

Check next item 4

4 in seen, add 4 to duplicates -> duplicates = {2, 4}

8

Check next item 4

4 in seen, add 4 to duplicates (already there) -> duplicates = {2, 4}

ItemSeen SetDuplicates Set
1{1}{}
2{1, 2}{}
2{1, 2}{2}
3{1, 2, 3}{2}
4{1, 2, 3, 4}{2}
4{1, 2, 3, 4}{2, 4}
4{1, 2, 3, 4}{2, 4}
💡

Why This Works

Step 1: Track seen items

We use a seen set to remember which items we have already checked.

Step 2: Identify duplicates

If an item is already in seen, it means it appeared before, so we add it to duplicates.

Step 3: Return duplicates

At the end, the duplicates set contains all items that appeared more than once.

🔄

Alternative Approaches

Using list count method
python
def find_duplicates(lst):
    return set(x for x in lst if lst.count(x) > 1)

print(find_duplicates([1, 2, 2, 3, 4, 4, 4]))
Simple but inefficient for large lists because <code>count</code> scans the list each time.
Using collections.Counter
python
from collections import Counter

def find_duplicates(lst):
    counts = Counter(lst)
    return {item for item, count in counts.items() if count > 1}

print(find_duplicates([1, 2, 2, 3, 4, 4, 4]))
Efficient and clean, uses a built-in tool to count occurrences.

Complexity: O(n) time, O(n) space

Time Complexity

The main method loops through the list once, so it takes O(n) time where n is the list size.

Space Complexity

It uses extra sets to store seen items and duplicates, so space is O(n) in the worst case.

Which Approach is Fastest?

Using collections.Counter or tracking seen items with sets is faster than counting each item repeatedly.

ApproachTimeSpaceBest For
Tracking seen items with setsO(n)O(n)Large lists, efficient
Using list count methodO(n²)O(n)Small lists, simple code
Using collections.CounterO(n)O(n)Clean and efficient counting
💡
Use a set to track seen items for an efficient way to find duplicates.
⚠️
Counting duplicates inside a loop without tracking seen items causes repeated duplicates in the result.