Skip to content

Instantly share code, notes, and snippets.

@wayneseymour
Last active February 18, 2021 21:26
Show Gist options
  • Select an option

  • Save wayneseymour/b5900b0751907faaf9205ee5327631f0 to your computer and use it in GitHub Desktop.

Select an option

Save wayneseymour/b5900b0751907faaf9205ee5327631f0 to your computer and use it in GitHub Desktop.
simplest unique array sort algo in short form
//Task: Transform this simple sorting algorithm into a unique sort.
// It should not return any duplicate values in the sorted array.
//input: [1,5,2,1] => output: [1,2,5]
//input: [4,2,2,3,2,2,2] => output: [2,3,4]
const uniqSort = obj => arr =>
Object
.keys(arr.reduce((acc, curr) => {
acc.i ? null : acc[curr] = true
return acc
}, {}))
.sort((a, b) => a - b)
uniqSort({})([4,2,2,3,2,2,2]); // => [2,3,4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment