-1
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..

zedfoxus
  • 35,121
  • 5
  • 64
  • 63
  • 2
    `if item not in list(set(a))` I'd just like to understand your reasoning for this line of code. – cs95 Apr 27 '19 at 04:19
  • 2
    Exact dupe of [How do I find the duplicates in a list and create another list with them?](https://stackoverflow.com/questions/9835762/how-do-i-find-the-duplicates-in-a-list-and-create-another-list-with-them) – Austin Apr 27 '19 at 04:24
  • Do you want new list to have duplicates (e.g. 1, 2) or do you want unique values to be in the new list (e.g. 1, 2, 4, 21, 5, 6, 7, 0) or do you want non-duplicates to be in the new list (e.g. 4, 21, 5, 6, 7, 0)? – zedfoxus Apr 27 '19 at 04:25

1 Answers1

0

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]
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40