Python Program to print duplicates from a list of integers
In this tutorial, we will see how we can print duplicates from a list of integers in Python. The List is an ordered set of values enclosed in square brackets [ ]. List stores some values called elements in it, which can be accessed by their particular index.
A duplicate element in a list is the element that has occurred more than once in the list. We can print duplicates by following various approaches.
Given a list of integers, we have to print all those integers which have occurred more than once in the list.
Input: [10, 2, 2, 3, 1, 6, -3, -2, 3, 4, 8, 10]
Output: [10, 2, 3]
For printing duplicate integers from a list in Python, we can follow these approaches:
- Brute force approach
- using Counter() method
Approach 1: Brute force approach
Also known as exhaustive search, in this, we will use two for loops which will count the occurrence of each integer in the list and display the duplicate elements based on the count of their occurrence.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Define a function to find duplicates
Step 2- In the function declare a list that will store all the duplicate integers
Step 3- Run a loop for each element in the list
Step 4- For each integer, run another loop that will check if the same integer is repeated
Step 5- If found the repeated integer will be added to another list using append()
Step 6- Return the list which stores repeated integers
Python Program 1
Look at the program to understand the implementation of the above-mentioned approach. In this program, we have defined a function that will check for each integer in the list if it is repeated in the list or not. Integers that are repeated will be stored in another list and returned by the function.
# print duplicates from a list of integers
#function
def duplicate(li):
n=len(li)
dup=[]
for i in range(n):
k = i + 1
for j in range(k,n):
if li[i] == li[j] and li[i] not in dup:
dup.append(li[i])
return dup
#test
li=[ 10, 20, 30, -10, -20, 10, 80, -10, -20, 30]
print("Duplicate integeres: ",duplicate(li))
Duplicate integeres: [10, 30, -10, -20]
Approach 2: Counter() function
Counter() is a built-in function that returns a dictionary of all the elements and their occurrences in a list. A dictionary has keys and values corresponding to it, the elements in the list and their occurrences are returned in a similar way.
After getting occurrences of each integer, we will check which integers occurred more than once and then store them in another list.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Import Counter function
Step 2- Take input of list with integer values
Step 3- Use Counter() to calculate occurrences of all the numbers in the list
Step 4- Declare another list
Step 5- Use a list comprehension to store integers with occurrence more than 1 in the list
Step 6- Print the list
Python Program 2
To use the Counter() function in your program, import it from the collections module. We will add all the numbers which are repeated in the list into another list with the help of list comprehension.
from collections import Counter
li=[1, 3, 2, 6, 2, 3, 5, 6, 4, 5]
d = Counter(li)
repeated_list = list([num for num in d if d[num]>1])
print("Duplicate integers: ",repeated_list)
Duplicate integers: [3, 2, 6, 5]
Conclusion
In this tutorial, we have discussed the two ways for finding and displaying duplicate elements in a list in Python. We have discussed the Brute force approach and also the Counter() approach for solving this task.