Sort by Frequency

Published by Matt in

Create a function that takes an array of integers arr and sort the elements of the array 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.

Watch a quick demo on how Edabit works.