Basic: Find All Duplicate Elements


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:
  return duplicates

# call the function

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

Leave a Comment