Python | Program to print duplicates from a list of integers
Last Updated :
29 Mar, 2023
Given a list of integers with duplicate elements in it. The task is to generate another list, which contains only the duplicate elements. In simple words, the new list should contain elements that appear as more than one.
Examples:
Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
Output : output_list = [20, 30, -20, 60]
Input : list = [-1, 1, -1, 8]
Output : output_list = [-1]
Method 1: Using the Brute Force approach
Python3
def Repeat(x):
_size = len (x)
repeated = []
for i in range (_size):
k = i + 1
for j in range (k, _size):
if x[i] = = x[j] and x[i] not in repeated:
repeated.append(x[i])
return repeated
list1 = [ 10 , 20 , 30 , 20 , 20 , 30 , 40 ,
50 , - 20 , 60 , 60 , - 20 , - 20 ]
print (Repeat(list1))
|
Time complexity: O(n^2) where n is the length of the input list
Auxiliary space: O(k) where k is the number of duplicates in the input list.
Method 2: Using a single for loop
Python3
lis = [ 1 , 2 , 1 , 2 , 3 , 4 , 5 , 1 , 1 , 2 , 5 , 6 , 7 , 8 , 9 , 9 ]
uniqueList = []
duplicateList = []
for i in lis:
if i not in uniqueList:
uniqueList.append(i)
elif i not in duplicateList:
duplicateList.append(i)
print (duplicateList)
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3: Using Counter() function from collection module
Python3
from collections import Counter
l1 = [ 1 , 2 , 1 , 2 , 3 , 4 , 5 , 1 , 1 , 2 , 5 , 6 , 7 , 8 , 9 , 9 ]
d = Counter(l1)
print (d)
new_list = list ([item for item in d if d[item]> 1 ])
print (new_list)
|
Output
Counter({1: 4, 2: 3, 5: 2, 9: 2, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1})
[1, 2, 5, 9]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4: Using count() method
Python3
list = [ 1 , 2 , 1 , 2 , 3 , 4 , 5 , 1 , 1 , 2 , 5 , 6 , 7 , 8 , 9 , 9 ]
new = []
for a in list :
n = list .count(a)
if n > 1 :
if new.count(a) = = 0 :
new.append(a)
print (new)
|
Method 5: Using list comprehension method
Python3
def duplicate(input_list):
return list ( set ([x for x in input_list if input_list.count(x) > 1 ]))
if __name__ = = '__main__' :
input_list = [ 1 , 2 , 1 , 2 , 3 , 4 , 5 , 1 , 1 , 2 , 5 , 6 , 7 , 8 , 9 , 9 ]
print (duplicate(input_list))
|
Method 6: Using list-dictionary approach (without any inbuild count function)
Python3
def duplicate(input_list):
new_dict, new_list = {}, []
for i in input_list:
if not i in new_dict:
new_dict[i] = 1
else :
new_dict[i] + = 1
for key, values in new_dict.items():
if values > 1 :
new_list.append(key)
return new_list
if __name__ = = '__main__' :
input_list = [ 1 , 2 , 1 , 2 , 3 , 4 , 5 , 1 , 1 , 2 , 5 , 6 , 7 , 8 , 9 , 9 ]
print (duplicate(input_list))
|
Method 7: Using in, not in operators and count() method
Python3
lis = [ 1 , 2 , 1 , 2 , 3 , 4 , 5 , 1 , 1 , 2 , 5 , 6 , 7 , 8 , 9 , 9 ]
x = []
y = []
for i in lis:
if i not in x:
x.append(i)
for i in x:
if lis.count(i) > 1 :
y.append(i)
print (y)
|
Method 8: Using enumerate function
Python3
input_list = [ 1 , 2 , 1 , 2 , 3 , 4 , 5 , 1 , 1 , 2 , 5 , 6 , 7 , 8 , 9 , 9 ]
print ( list ( set ([x for i,x in enumerate (input_list) if input_list.count(x) > 1 ])))
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 9: Using operator.countOf() method
Python3
import operator as op
list = [ 1 , 2 , 1 , 2 , 3 , 4 , 5 , 1 , 1 , 2 , 5 , 6 , 7 , 8 , 9 , 9 ]
new = []
for a in list :
n = op.countOf( list , a)
if n > 1 :
if op.countOf(new, a) = = 0 :
new.append(a)
print (new)
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 10 : Using operator.countOf(),in and not in operators
Python3
lis = [ 1 , 2 , 1 , 2 , 3 , 4 , 5 , 1 , 1 , 2 , 5 , 6 , 7 , 8 , 9 , 9 ]
x = []
y = []
import operator
for i in lis:
if i not in x:
x.append(i)
for i in x:
if operator.countOf(lis,i) > 1 :
y.append(i)
print (y)
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 11 : Using numpy:
Algorithm:
- Initialize the list l1 with some values.
- Import the numpy library.
- Use numpy.unique() to get the unique values and their counts in the list l1.
- Use numpy.where() to get the indices of the elements in the counts array that are greater than 1.
- Use these indices to get the corresponding elements from the unique array.
Python3
import numpy as np
l1 = [ 1 , 2 , 1 , 2 , 3 , 4 , 5 , 1 , 1 , 2 , 5 , 6 , 7 , 8 , 9 , 9 ]
unique, counts = np.unique(l1, return_counts = True )
new_list = unique[np.where(counts > 1 )]
print (new_list)
|
Output:
[1 2 5 9]
Time Complexity:
numpy.unique() has a time complexity of O(nlogn) due to the sorting step, where n is the length of the input array.
numpy.where() has a time complexity of O(n), where n is the length of the input array.
Indexing an array takes constant time O(1).
Therefore, the time complexity of this numpy code is O(nlogn), dominated by numpy.unique().
Auxiliary Space:
The space complexity of this numpy code is O(n), since we are storing the input list l1 and the output list new_list.
numpy.unique() and numpy.where() internally create intermediate arrays, but they are not included in the space complexity of this code because they are temporary and not stored in memory.
Please Login to comment...