Chalarangelo/30-seconds-of-python

[FEATURE] Improve filter_unique and filter_non_unique running time

mx-psi opened this issue · 1 comments

The filter_non_unique and filter_unique snippets currently have a quadratic running time; in both cases they count each item in the original list. This can be improved to linear time by using a counter from the collections module (at the expense of linear space).

Improvement of filter_unique

from collections import Counter


def filter_unique(lst):
  counter = Counter(lst)
  return [item for item, count in counter.items() if count > 1]

Improvement of filter_non_unique

from collections import Counter


def filter_non_unique(lst):
  counter = Counter(lst)
  return [item for item, count in counter.items() if count == 1]

If you can PR these changes along with their explanations, I'd be glad to merge them. Thanks for the feedback!