← Back to challenges

Sort by Frequency

JavaScriptHardsortingnumbers

Instructions

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

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

sortFreq([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]

sortFreq([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.

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