Last active
August 29, 2020 07:00
-
-
Save beetcb/160a383fc7208447ef538fbdf963154f to your computer and use it in GitHub Desktop.
Array Deduplication
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
array.filter(function (item, position) { | |
return array.indexOf(item) == position | |
}) |
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
const uniqueArray = [] | |
array.forEach(n => { | |
uniqueArray.indexOf(n) === -1 && uniqueArray.push(n) | |
}) |
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
let map = null | |
const uniqueArray = [] | |
map = array.reduce((map, val) => { | |
map[val] = null | |
return map | |
}, new Map()) | |
for (let key in map) { | |
uniqueArray.push(key) | |
} |
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
// De-duplication | |
array.reduce((a, val) => { | |
a.indexOf(val) === -1 && a.push(val) | |
return a | |
}, []) |
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
const uniqueArray = [...new Set(array)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment