Python Program to find Duplicate sets in list of sets

When working with lists of sets, you may need to identify which sets appear more than once. Python provides an efficient solution using Counter from the collections module and frozenset to handle duplicate sets.

Why Use frozenset?

Sets are mutable and unhashable, so they cannot be directly counted by Counter. frozenset creates an immutable, hashable version of a set that can be used as a dictionary key or counted.

Example

Below is a demonstration of finding duplicate sets ?

from collections import Counter

my_list = [{4, 8, 6, 1}, {6, 4, 1, 8}, {1, 2, 6, 2}, {1, 4, 2}, {7, 8, 9}]

print("The list is :")
print(my_list)

my_freq = Counter(frozenset(sub) for sub in my_list)

my_result = []
for key, value in my_freq.items():
    if value > 1:
        my_result.append(key)

print("The result is :")
print(my_result)
The list is :
[{8, 1, 4, 6}, {8, 1, 4, 6}, {1, 2, 6}, {1, 2, 4}, {8, 9, 7}]
The result is :
[frozenset({8, 1, 4, 6})]

Alternative Method: List Comprehension

You can achieve the same result more concisely using list comprehension ?

from collections import Counter

my_list = [{4, 8, 6, 1}, {6, 4, 1, 8}, {1, 2, 6, 2}, {1, 4, 2}, {7, 8, 9}]

my_freq = Counter(frozenset(sub) for sub in my_list)
duplicates = [key for key, value in my_freq.items() if value > 1]

print("Duplicate sets:")
print(duplicates)
Duplicate sets:
[frozenset({8, 1, 4, 6})]

How It Works

The algorithm follows these steps:

  • Convert each set to a frozenset to make it hashable

  • Use Counter to count occurrences of each frozenset

  • Filter frozensets with frequency greater than 1

  • Return the duplicate frozensets

Converting Back to Sets

If you need the result as regular sets instead of frozensets ?

from collections import Counter

my_list = [{4, 8, 6, 1}, {6, 4, 1, 8}, {1, 2, 6, 2}, {1, 4, 2}, {7, 8, 9}]

my_freq = Counter(frozenset(sub) for sub in my_list)
duplicates = [set(key) for key, value in my_freq.items() if value > 1]

print("Duplicate sets as regular sets:")
print(duplicates)
Duplicate sets as regular sets:
[{8, 1, 4, 6}]

Conclusion

Use Counter with frozenset to efficiently find duplicate sets in a list. This approach leverages the hashable nature of frozensets to count occurrences and identify duplicates.

Updated on: 2026-03-26T03:03:10+05:30

428 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements