Use collections.Counter, get the ones with count of 1:
from collections import Counter
def find_uniq(arr):
c = Counter(arr)
return [number for number,count in c.most_common() if count == 1]
print(find_uniq( [1,2,3,4,2,3,4,5,6,4,5,6,7,8,9])) # [1, 7, 8, 9]
This takes about O(2*n) so O(n) as 2 is constant.
collection.defaultdict with int, get the ones with count of 1:
# defaultdict
from collections import Counter , defaultdict
def find_uniq(arr):
c = defaultdict(int)
for a in arr:
c[a] += 1
return [number for number,count in c.items() if count == 1]
print(find_uniq( [1,2,3,4,2,3,4,5,6,4,5,6,7,8,9])) # [1, 7, 8, 9]
This takes about O(2*n) so O(n) as 2 is constant - it is slighty faster then Counter because of C-optimizations inside the implementation (see f.e. Surprising results with Python timeit: Counter() vs defaultdict() vs dict()).
normal dicts and setdefault or test/add, get the ones with count of 1:
# normal dict - setdefault
def find_uniq(arr):
c = dict()
for a in arr:
c.setdefault(a,0)
c[a] += 1
return [number for number,count in c.items() if count == 1]
print(find_uniq( [1,2,3,4,2,3,4,5,6,4,5,6,7,8,9])) # [1, 7, 8, 9]
# normal dict - test and add
def find_uniq(arr):
c = dict()
for a in arr:
if a in c:
c[a] += 1
else:
c[a] = 1
return [number for number,count in c.items() if count == 1]
print(find_uniq( [1,2,3,4,2,3,4,5,6,4,5,6,7,8,9])) # [1, 7, 8, 9]
Setdefault creates the defaultvalue every time - it is slower then Counter or defaultdict and faster then using test/add.
itertools.groupby (needs sorted list!), get the ones with count of 1:
from itertools import groupby
def find_uniq(arr):
return [k for (k,p) in groupby(sorted(arr)) if len(list(p)) == 1]
print(find_uniq( [1,2,3,4,2,3,4,5,6,4,5,6,7,8,9])) # [1, 7, 8, 9]
groupby needs a sorted list, list sort alone is O(n * log n) and in combination this is slower then the other approaches.