Skip to content

Instantly share code, notes, and snippets.

@paulredmond
Created July 26, 2023 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulredmond/f11e5ad4ac71dc95f76bfb56f8ab624c to your computer and use it in GitHub Desktop.
Save paulredmond/f11e5ad4ac71dc95f76bfb56f8ab624c to your computer and use it in GitHub Desktop.
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
})
}
@paulredmond
Copy link
Author

With the following input,

items = [4, 5, 6, 5, 4, 3]

expected output:

[3, 6, 4,  4, 5, 5]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment