If elements are always nearly sorted as in your example then builtin .sort()
(timsort) should be fast:
>>> a = [1,1,2]
>>> b = [1,2,2]
>>> a.sort()
>>> b.sort()
>>> a == b
False
If you don't want to sort inplace you could use sorted()
.
In practice it might always be faster then collections.Counter()
(despite asymptotically O(n)
time being better then O(n*log(n))
for .sort()
). Measure it; If it is important.