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]
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
Improvement of filter_non_unique