Problem 6.1

Find Duplicates

Write a function dups that takes a list of values as argument and finds all the elements that appear more than once in the list.

>>> dups([1, 2, 1, 3, 2, 5])
[1, 2]
>>> dups([1, 2, 3, 4, 5])
[]
>>> dups([1, 1, 1, 1])
[1]

Solution

def dups(numbers):
    seen = []
    result = []
    for n in numbers:
        if n in seen and n not in result:
            result.append(n)
        seen.append(n)
    return result