Open In App

Python Program to Find Duplicate sets in list of sets

Last Updated : 18 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Step-by-step approach:

  • Import the necessary modules: Counter from the collections module as this will be used to count the frequency of items in the list.
  • Initialize a list test_list
  • Each set has some integers in it.
  • Print the original list using print().
  • Use a list comprehension to create a new list freqs of frozen sets. Each frozen set is obtained by iterating through test_list, converting each set into a frozen set using frozenset().
  • The Counter() function is then used to count the frequency of each frozen set.
  • Initialize an empty list res.
  • Iterate through the items in freqs using a for loop.
    • For each item, extract the key and value.
      • If the value is greater than 1, the key is a duplicate set, so it is appended to res.
  • Print the final result using print().
  • The program execution is complete.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate working of
# Duplicate sets in list of sets
# Using Counter() + count() + frozenset() + loop
from collections import Counter
 
# initializing list
test_list = [{4, 5, 6, 1}, {6, 4, 1, 5}, {1, 3, 4, 3},
             {1, 4, 3}, {7, 8, 9}]
              
# printing original list
print("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 result
print("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})]

Time complexity: O(n), where n is the length of the input list. 
Auxiliary space: O(n), where n is the length of the input list. 

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.

  1. Import the Counter class from the collections module.
  2. Initialize a list of sets called test_list with 5 sets containing different elements.
  3. Print the original list.
  4. Use a list comprehension to create a new list of frozen sets freqs that contains the frequency of each set in test_list.
    *For each set sub in test_list, a frozen set is created and added to freqs using the Counter() function.
  5. Use another list comprehension to create a new list res that contains only the sets from freqs that occur more than once.
    *For each key, value pair in freqs, the key is added to res only if the value is greater than 1.
  6. Print the final result, which is a list of all the sets in test_list that occur more than once.

Python3




# Python3 code to demonstrate working of
# Duplicate sets in list of sets
# Using list comprehension + Counter()
from collections import Counter
 
# initializing list
test_list = [{4, 5, 6, 1}, {6, 4, 1, 5}, {1, 3, 4, 3}, {1, 4, 3}, {7, 8, 9}]
              
# printing original list
print("The original list is : " + str(test_list))
 
# getting frequency using Counter()
freqs = Counter(frozenset(sub) for sub in test_list)
 
# list comprehension provides shorthand solution
res = [key for key, val in freqs.items() if val > 1]
 
# printing result
print("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})]

Time Complexity: O(n), where n is the length of the test_list.
Auxiliary Space: O(n), where n is the length of the test_list.

Method #3 : Using nested loop

Python3




# initializing list
test_list = [{4, 5, 6, 1}, {6, 4, 1, 5}, {1, 3, 4, 3},
             {1, 4, 3}, {7, 8, 9}]
# printing original list
print("The original list is : " + str(test_list))
# creating an empty set to store the result
result = set()
# nested for loop to compare sets
for i in range(len(test_list)):
    for j in range(i+1, len(test_list)):
        if test_list[i] == test_list[j]:
            result.add(frozenset(test_list[i]))
# printing result
print("Duplicate sets list : " + str(list(result)))
#This code is contributed by Jyothi pinjala


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})]

Time Complexity: O(n^2)
Auxiliary Space: O(n)

Method #4: Using set() and filter()

This method uses the set() function to get a set of unique sets. The map() function is used to convert each set in test_list to a frozenset, which is hashable and can be used as a key in a set or dictionary. The filter() function is then used to filter out the unique sets and return only the duplicate sets. The lambda function checks if the count of the set in test_list is greater than 1. Finally, the list of duplicate sets is printe

Python3




# Python3 code to demonstrate working of
# Duplicate sets in list of sets
# Using set() and filter()
 
# initializing list
test_list = [{4, 5, 6, 1}, {6, 4, 1, 5}, {1, 3, 4, 3},
             {1, 4, 3}, {7, 8, 9}]
              
# printing original list
print("The original list is : " + str(test_list))
 
# using set() to get unique sets
unique_sets = set(map(frozenset, test_list))
 
# using filter() to get duplicate sets
duplicate_sets = list(filter(lambda s: test_list.count(s) > 1, unique_sets))
 
# printing result
print("Duplicate sets list : " + str(duplicate_sets))


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})]

Time Complexity: The time complexity of this code is O(n^2), where n is the length of the input list test_list. 
Auxiliary Space: The auxiliary space complexity of this code is O(n), where n is the length of the input list test_list. 

Method #5: Using defaultdict

This approach uses a defaultdict to store the sets and their counts. Loop through the input list of sets and add each set to the defaultdict. The frozenset is used as the key since sets are not hashable, but frozensets are. Then loop through the defaultdict to get the sets with count greater than 1, indicating duplicates. The result is a list of duplicate sets.

Python3




from collections import defaultdict
 
test_list = [{4, 5, 6, 1}, {6, 4, 1, 5}, {1, 3, 4, 3}, {1, 4, 3}, {7, 8, 9}]
 
# create defaultdict to store sets with their count
set_counts = defaultdict(int)
for s in test_list:
    set_counts[frozenset(s)] += 1
 
# get sets with count greater than 1
duplicate_sets = [set for set, count in set_counts.items() if count > 1]
 
# print result
print("Duplicate sets list : " + str(duplicate_sets))


Output

Duplicate sets list : [frozenset({1, 4, 5, 6}), frozenset({1, 3, 4})]

Time complexity: O(N*M), where N is the length of test_list and M is the size of the largest set in the list.
Auxiliary space: O(N*M), where N is the length of test_list and M is the size of the largest set in the list.

Method #6: Using Hashing with Dictionary

Use a dictionary to keep track of the sets that we have already encountered while iterating through the list. We will add the sets to the dictionary as keys and set their values to 1. If we come across a set that is already in the dictionary, we will append it to the result list.

Python3




# Python3 code to demonstrate working of
# Duplicate sets in list of sets
# Using Hashing with Dictionary
 
# initializing list
test_list = [{4, 5, 6, 1}, {6, 4, 1, 5}, {1, 3, 4, 3},
             {1, 4, 3}, {7, 8, 9}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using dictionary to check for duplicates
hash_dict = {}
res = []
for s in test_list:
    if frozenset(s) in hash_dict:
        # set already exists in dictionary, so it is a duplicate
        res.append(s)
    else:
        # add set to dictionary
        hash_dict[frozenset(s)] = 1
 
# printing result
print("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 : [{1, 4, 5, 6}, {1, 3, 4}]

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.



Previous Article
Next Article

Similar Reads

Python program to remove duplicate elements index from other list
Given two lists, the task is to write a Python program to remove all the index elements from 2nd list which are duplicate element indices from 1st list. Examples: Input : test_list1 = [3, 5, 6, 5, 3, 7, 8, 6], test_list2 = [1, 7, 6, 3, 7, 9, 10, 11] Output : [1, 7, 6, 9, 10] Explanation : 3, 7 and 11 correspond to 2nd occurrence of 5, 3 and 6, henc
7 min read
Python - Convert List of lists to list of Sets
Given a list containing lists, the task is to write a Python Program to convert it to a list containing sets. Examples: Input : [[1, 2, 1], [1, 2, 3], [2, 2, 2, 2], [0]]Output : [{1, 2}, {1, 2, 3}, {2}, {0}]Input : [[4, 4], [5, 5, 5], [1, 2, 3]]Output : [{4}, {5}, {1, 2, 3}]Python Convert List of lists to list of SetsBelow are the ways by which we
4 min read
Find the Union on List of Sets in Python
The union of sets involves combining distinct elements from multiple sets into a single set, eliminating duplicates. In this article, we will study how to find the union on a list of sets in Python. Find the Union on a List of Sets in PythonBelow are some of the ways by which we can find the union on a list of sets in Python: Using union() method i
4 min read
Python program to find common elements in three lists using sets
Prerequisite: Sets in Python Given three arrays, we have to find common elements in three sorted lists using sets. Examples : Input : ar1 = [1, 5, 10, 20, 40, 80] ar2 = [6, 7, 20, 80, 100] ar3 = [3, 4, 15, 20, 30, 70, 80, 120] Output : [80, 20] Input : ar1 = [1, 5, 5] ar2 = [3, 4, 5, 5, 10] ar3 = [5, 5, 10, 20] Output : [5] Method 1: We have given
5 min read
Python | Remove duplicate tuples from list of tuples
Given a list of tuples, Write a Python program to remove all the duplicated tuples from the given list. Examples: Input : [(1, 2), (5, 7), (3, 6), (1, 2)] Output : [(1, 2), (5, 7), (3, 6)] Input : [('a', 'z'), ('a', 'x'), ('z', 'x'), ('a', 'x'), ('z', 'x')] Output : [('a', 'z'), ('a', 'x'), ('z', 'x')] Method #1 : List comprehension This is a naive
5 min read
Python | Remove unordered duplicate elements from a list
Given a list, the task is to remove the duplicate elements. All the elements which are not in same order but made of same characters/numbers are considered as duplicates. Examples: Input : ['gfg', 'ggf', 'fgg', 'for', 'orf', 'ofr', 'rfo', 'rof', 'fro'] Output : ['for', 'fgg'] Input: ['110', '101', '001', '010', '100'] Output: ['001', '011'] Method
2 min read
Python | Remove tuples having duplicate first value from given list of tuples
Given a list of tuples, the task is to remove all tuples having duplicate first values from the given list of tuples. Examples: Input: [(12.121, 'Tuple1'), (12.121, 'Tuple2'), (12.121, 'Tuple3'), (923232.2323, 'Tuple4')] Output: [(12.121, 'Tuple1'), (923232.2323, 'Tuple4')]Input: [('Tuple1', 121), ('Tuple2', 125), ('Tuple1', 135), ('Tuple4', 478)]
7 min read
Python | Duplicate substring removal from list
Sometimes we can come to the problem in which we need to deal with certain strings in a list that are separated by some separator and we need to remove the duplicates in each of these kinds of strings. Simple shorthands to solve this kind of problem is always good to have. Let's discuss certain ways in which this can be done. Method #1: Using split
7 min read
Python | Altering duplicate values from given list
Many times we deal with the lists having identical numbers as a sequence and we wish to just keep the 1st occurrence of element and substituting all the occurrences with the different number. Let's discuss certain ways in which this can be done. Method #1 : Using list comprehension + enumerate() This task can be achieved using the list comprehensio
5 min read
Python | Duplicate element indices in list
While working with Python list, sometimes, we require to check for duplicates and may also sometimes require to track their indices. This kind of application can occur in day-day programming. Let's discuss certain ways in which this problem can be solved. Method #1 : Using loop + set() This task can be solved using the combination of above function
3 min read