def dups(a):
emp = []
for item in a:
if item not in list(set(a)):
emp.append(item)
return emp
b = [1, 2, 2, 4, 21, 5, 2, 6, 1, 7, 0]
print(dups(b))
this gives me an empty array and I don't know why..
def dups(a):
emp = []
for item in a:
if item not in list(set(a)):
emp.append(item)
return emp
b = [1, 2, 2, 4, 21, 5, 2, 6, 1, 7, 0]
print(dups(b))
this gives me an empty array and I don't know why..
If you print the value of list(set(a)
you get [0, 1, 2, 4, 5, 6, 7, 21]
, which are all the elements in a
, so when you do if item not in list(set(a))
, you are bound to get an empty list, since that condition eliminates all items.
A better approach is to find the count of all elements in the list, and duplicates will be all the elements which has count >1
as follows.
from collections import Counter
def dups(a):
emp = []
#This gives you a dictionary of number to frequency of occurence
counter = Counter(a)
#Counter({2: 3, 1: 2, 4: 1, 21: 1, 5: 1, 6: 1, 7: 1, 0: 1})
#Iterate through the dict, and if freq is more that 1, it is duplicate
for k,v in counter.items():
if v > 1:
emp.append(k)
return emp
b = [1, 2, 2, 4, 21, 5, 2, 6, 1, 7, 0]
print(dups(b))
The expected output will be:
[1, 2]