Sort arr by elem frequency: fail on final assert
import collections def frequency_sort(items): if items == []: return items counts = collections.Counter(items) for i in counts: if counts[i] > 1: new_list = sorted(items, key=lambda x: (counts[x], x), reverse=True) return new_list else: return items
Hi all! This code from "Sort Array by Element Frequency" fails on the final assert, where frequency_sort([1,2,2,1]). It returns [2,2,1,1], instead required [1,1,2,2] and I don't know why it happened particularly here and how to fix that. Thanks!