← Back to challenges

Sort by Frequency

PythonHardsortingnumbers

Instructions

Create a function that takes a list of integers lst and sort the elements of the list by decreasing frequency of the elements. If two elements have the same frequency, sort them by increasing value.

Examples

sort_freq([2, 3, 5, 3, 7, 9, 5, 3, 7]) ➞ [3, 3, 3, 5, 5, 7, 7, 2, 9]

sort_freq([1, 2, 3, 0, 5, 0, 1, 6, 8, 8, 6, 9, 1]) ➞ [1, 1, 1, 0, 0, 6, 6, 8, 8, 2, 3, 5, 9]

sort_freq([4, 4, 2, 5, 1, 1, 3, 3, 2, 8]) ➞ [1, 1, 2, 2, 3, 3, 4, 4, 5, 8]

Notes

All input numbers will be between 0-9.

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.