Last active
February 18, 2021 21:26
-
-
Save wayneseymour/b5900b0751907faaf9205ee5327631f0 to your computer and use it in GitHub Desktop.
simplest unique array sort algo in short form
This file contains hidden or 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
| //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