Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Counter solution in Clear category for Sort Array by Element Frequency by nurzhan7511
from collections import Counter
from itertools import chain
from typing import Iterable
def frequency_sort(items: list[str | int]) -> Iterable[str | int]:
c = Counter(items)
return chain(*[[item] * c[item] for item in sorted(set(items), key=lambda i: (-c[i], items.index(i)))])
Oct. 29, 2023
Comments: