Python Program to Find Duplicate sets in list of sets
Given a list of sets, the task is to write a Python program to find duplicate sets.
Input : test_list = [{4, 5, 6, 1}, {6, 4, 1, 5}, {1, 3, 4, 3}, {1, 4, 3}, {7, 8, 9}]
Output : [frozenset({1, 4, 5, 6}), frozenset({1, 3, 4})]
Explanation : {1, 4, 5, 6} is similar to {6, 4, 1, 5} hence part of result.
Input : test_list = [{4, 5, 6, 9}, {6, 4, 1, 5}, {1, 3, 4, 3}, {1, 4, 3}, {7, 8, 9}]
Output : [frozenset({1, 3, 4})]
Explanation : {1, 3, 4} ({1, 3, 4, 3}) is similar to {1, 4, 3} hence part of result.
Method #1 : Using Counter() + count() + frozenset() + loop
In this, all the sets are hashed by converting them into frozenset() [ to get hashable type ] into frequency using Counter(). Then count() is used to get count of all the present sets from frequency counter created.
Python3
# Python3 code to demonstrate working of# Duplicate sets in list of sets# Using Counter() + count() + frozenset() + loopfrom collections import Counter # initializing listtest_list = [{4, 5, 6, 1}, {6, 4, 1, 5}, {1, 3, 4, 3}, {1, 4, 3}, {7, 8, 9}] # printing original listprint("The original list is : " + str(test_list)) # getting frequency using Counter()freqs = Counter(frozenset(sub) for sub in test_list) res = []for key, val in freqs.items(): # if frequency greater than 1, set is appended # [duplicate] if val > 1 : res.append(key) # printing resultprint("Duplicate sets list : " + str(res)) |
Output:
The original list is : [{1, 4, 5, 6}, {1, 4, 5, 6}, {1, 3, 4}, {1, 3, 4}, {8, 9, 7}]
Duplicate sets list : [frozenset({1, 4, 5, 6}), frozenset({1, 3, 4})]
Method #2 : Using list comprehension + Counter()
In this, we perform similar task, only difference being list comprehension is used as one liner to extract duplicates based on frequency dictionary.
Python3
# Python3 code to demonstrate working of# Duplicate sets in list of sets# Using list comprehension + Counter()from collections import Counter # initializing listtest_list = [{4, 5, 6, 1}, {6, 4, 1, 5}, {1, 3, 4, 3}, {1, 4, 3}, {7, 8, 9}] # printing original listprint("The original list is : " + str(test_list)) # getting frequency using Counter()freqs = Counter(frozenset(sub) for sub in test_list) # list comprehension provides shorthand solutionres = [key for key, val in freqs.items() if val > 1] # printing resultprint("Duplicate sets list : " + str(res)) |
Output:
The original list is : [{1, 4, 5, 6}, {1, 4, 5, 6}, {1, 3, 4}, {1, 3, 4}, {8, 9, 7}]
Duplicate sets list : [frozenset({1, 4, 5, 6}), frozenset({1, 3, 4})]


