Created
July 26, 2023 21:39
-
-
Save paulredmond/f11e5ad4ac71dc95f76bfb56f8ab624c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function itemsSort(items) { | |
// Write your code here | |
const countObj = items.reduce((rlt, val) => { | |
if (val in rlt) { | |
rlt[val]++ | |
} else { | |
rlt[val] = 1 | |
} | |
return rlt; | |
}, {}) | |
return items.sort((num1, num2) => { | |
if (countObj[num1] !== countObj[num2]) return countObj[num1] - countObj[num2] | |
return num1 - num2 | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With the following input,
expected output: