Please write a function to find all duplicate elements in a list. Input: a list of integers. Output: a set of integers that occur twice or more in the input list.
We keep tracking the unique elements in a set. The next time we encounter any of those elements again, we put them in the result set.
def duplicate_elements(input): # two sets: one to store unique elements and # another one to store the duplicates unique = set() duplicates = set() for i in input: if i not in elements: elements.add(i) else: duplicates.add(i) return duplicates # call the function print(duplicate_elements([8,2,3,4,1,2,3,5,0,8]))
👍 Have fun while coding with Python!
👌 Please subscribe to receive notifications on future challenges.
💬 If you have any questions simply write a comment down below.