Skip to content

Instantly share code, notes, and snippets.

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