0

For context: Users can add items to a list. I want to remove all identical submissions. Is there a more efficient way to do this:?

def removeCopies(self):
    i = 0
    while i != self.size:
        number = self.contents.count(self.contents[i])
        if number != 1:
            for j in range(1, number):
                self.delete(self.contents.index(self.contents[i], i + 1))
        i += 1
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264

1 Answers1

3

The most efficient way I can think of to delete duplicates is

self.contents = list(set(self.contents))

This will not preserve the order of your items, but at least you won't end up with an O(n3) algorithm.

An even better solution would be to make self.contents a set to begin with, so users can not enter duplicates at all, if you have the option to do that.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264