Open In App

Python | Program to count duplicates in a list of tuples

Improve
Improve
Like Article
Like
Save
Share
Report

Given a list of tuples, write a Python program to check if an element of the list has duplicates. If duplicates exist, print the number of occurrences of each duplicate tuple, otherwise print “No Duplicates”.

Examples:

Input  : [('a', 'e'), ('b', 'x'), ('b', 'x'), ('a', 'e'), ('b', 'x')] 
Output : ('a', 'e') - 2 ('b', 'x') - 3 

Input  : [(0, 5), (6, 9), (0, 8)] 
Output : No Duplicates

Let’s see the various ways we can count duplicates in a list of tuples. 

Approach #1: Naive Approach This approach uses two loops to traverse the list of elements and check if the first element and second element of each element match any other tuple. 

Python3




# Python3 code to convert tuple
# into string
 
 
def count(listOfTuple):
 
    flag = False
 
    # To append Duplicate elements in list
    coll_list = []
    coll_cnt = 0
    for t in listOfTuple:
 
        # To check if Duplicate exist
        if t in coll_list:
            flag = True
            continue
 
        else:
            coll_cnt = 0
            for b in listOfTuple:
                if b[0] == t[0] and b[1] == t[1]:
                    coll_cnt = coll_cnt + 1
 
            # To print count if Duplicate of element exist
            if(coll_cnt & gt
               1):
                print(t, & quot
                      - ", coll_cnt)
            coll_list.append(t)
 
    if flag == False:
        print(& quot
               No Duplicates & quot
               )
 
 
# Driver code
print(& quot
       Test Case 1: & quot
       )
listOfTuple = [('a', 'e'), ('b', 'x'), ('b', 'x'),
               ('a', 'e'), ('b', 'x')]
 
count(listOfTuple)
 
print(& quot
       Test Case 2: & quot
       )
listOfTuple = [(0, 5), (6, 9), (0, 8)]
count(listOfTuple)


Time complexity – O(n)2   

Approach #2: Using Counter is a container included in the collections module. It is an unordered collection where elements and their respective count are stored as dictionary. 

Python3




# Python3 code to convert tuple
# into string
import collections
 
 
def count(listOfTuple):
 
    flag = False
    val = collections.Counter(listOfTuple)
    uniqueList = list(set(listOfTuple))
 
    for i in uniqueList:
        if val[i] & gt
        = 2:
            flag = True
            print(i, & quot
                  - ", val[i])
 
    if flag == False:
        print(& quot
               Duplicate doesn't exist & quot
               )
 
 
# Driver code
listOfTuple = [('a', 'e'), ('b', 'x'), ('b', 'x'),
               ('a', 'e'), ('b', 'x')]
count(listOfTuple)


Time complexity – O(n)   

Approach #3: Using another dict You can make a dictionary, say count_map, and store the count of each tuple as the value. 

Python3




# Python3 code to convert tuple
# into string
 
 
def count(listOfTuple):
 
    count_map = {}
    for i in listOfTuple:
        count_map[i] = count_map.get(i, 0) + 1
    print(count_map)
 
 
# Driver code
print( & quot
      Test Case 1: & quot
      )
listOfTuple = [('a', 'e'), ('b', 'x'), ('b', 'x'),
               ('a', 'e'), ('b', 'x')]
 
count(listOfTuple)


Time complexity – O(n)

Approach #4:  Using collections

Another approach to count duplicates in a list of tuples is to use a Counter object from the collections module. You can pass the list of tuples to the Counter constructor and it will create a dictionary-like object that stores the count of each tuple. Then, you can iterate over the Counter object and print out the tuple and its count if the count is greater than 1.

Here is an example of how you could implement this approach:

Python3




from collections import Counter
 
def count_duplicates(lst):
    counter = Counter(lst)
    for tup, count in counter.items():
        if count > 1:
            print(f"{tup}: {count}")
    if not any(count > 1 for count in counter.values()):
        print("No Duplicates")
 
# Example usage
lst = [('a', 'e'), ('b', 'x'), ('b', 'x'), ('a', 'e'), ('b', 'x')]
count_duplicates(lst)  # prints ('a', 'e'): 2, ('b', 'x'): 3
 
lst = [(0, 5), (6, 9), (0, 8)]
count_duplicates(lst)  # prints No Duplicates
#This code is contributed by Edula Vinay Kumar Reddy


Output

('a', 'e'): 2
('b', 'x'): 3
No Duplicates

This approach has a time complexity of O(n), where n is the length of the input list, because it involves one pass through the list of tuples to create the Counter object and another pass through the Counter object to print the output. The space complexity is also O(n), because the Counter object stores a count for each tuple in the input list.

Approach#5: Using a dictionary

this approach uses a dictionary to count the frequency of tuples in the list. The tuples are used as keys in the dictionary and their frequency is counted using the get() method. Finally, the duplicates are printed from the dictionary.

Steps to follow in this algorithm:

  • Create an empty dictionary to store the count of each tuple.
  • Loop through each tuple in the list:
    • If the tuple is not in the dictionary, add it with a count of 1.
    • If the tuple is already in the dictionary, increment its count by 1.
  • Loop through the dictionary and print the tuples with their respective counts.

Python3




def count_duplicates(tuples_list):
    count_dict = {}
    for t in tuples_list:
        if t not in count_dict:
            count_dict[t] = 1
        else:
            count_dict[t] += 1
    duplicates = [(t, c) for t, c in count_dict.items() if c > 1]
    if len(duplicates) == 0:
        print("No Duplicates")
    else:
        for t, c in duplicates:
            print(f"{t} - {c}")
 
 
# Example usage
lst = [('a', 'e'), ('b', 'x'), ('b', 'x'), ('a', 'e'), ('b', 'x')]
count_duplicates(lst)  # prints ('a', 'e'): 2, ('b', 'x'): 3
 
lst = [(0, 5), (6, 9), (0, 8)]
count_duplicates(lst)  # prints No Duplicates


Output

('a', 'e') - 2
('b', 'x') - 3
No Duplicates

Time complexity: O(n), where n is the number of tuples in the list.
Space complexity: O(n), where n is the number of unique tuples in the list.

Approach#6: Using counter+lambda+filter

 Using Counter to count the occurrences of each element in the list of tuples, and then filtering the Counter object to identify the tuples that have duplicates.

Algorithm

1. Import the Counter class from the collections module.
2. Create a list of tuples called data.
3. Create a Counter object called counter by passing data as an argument to the Counter constructor.
4. Filter the counter object to create a list of tuples called duplicates that contains only the elements with a count greater than 1.
5. Print the duplicates list if it is not empty, or a message indicating that there are no duplicates.

Python3




from collections import Counter
 
data = [('a', 'e'), ('b', 'x'), ('b', 'x'), ('a', 'e'), ('b', 'x')]
 
counter = Counter(data)
 
duplicates = list(filter(lambda x: x[1]>1, counter.items()))
if duplicates:
    for item in duplicates:
        print(f'{item[0]} - {item[1]}')
else:
    print('No duplicates')


Output

('a', 'e') - 2
('b', 'x') - 3

Time Complexity: O(n), where n is the length of the list data, because we have to iterate over the list to create the Counter object.

Auxiliary Space: O(n), where n is the length of the list data, because we are creating a Counter object that contains all the elements of the list.



Last Updated : 17 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads