Python:Remove Duplicates from Array

I’m trying to address the duplicates in an array issue using Python. I’ve discovered some ideas online, but I’m not sure which is the most practical. Tutorial: Remove Duplicates from the Array, which I cited in my work.

One choice is to use a set. A set is a particular type of data structure that only stores solitary components. We can first create a set of the array’s components in order to generate an array without duplicate elements.

def remove_duplicates(array):
  seen = set()
  new_array = []
  for element in array:
    if element not in seen:
      seen.add(element)
      new_array.append(element)
  return new_array

Utilising a hash table is a further remedy. A hash table is a type of data structure that links keys and values. A hash table can be used to transform each element of the array into a distinct numeric value. We can then add hash table elements to the new array that only have unique numbers by repeating the process.

def remove_duplicates(array):
  seen = {}
  new_array = []
  for element in array:
    if element not in seen:
      seen[element] = 1
      new_array.append(element)
  return new_array

Which of these strategies is the best outcomes? Is there a better way to remove duplicates from an array in Python?

If your array contains elements that are hashable, just use something like array=list(set(array)).

The hash-table option is redundant, because sets use the same hashing mechanism under the hood. See: python - How is set() implemented? - Stack Overflow

1 Like