Skip to content

Instantly share code, notes, and snippets.

@tonis2
Created October 11, 2016 15:51
Show Gist options
  • Save tonis2/071e662ac50e809775e44ce93c1d426b to your computer and use it in GitHub Desktop.
Save tonis2/071e662ac50e809775e44ce93c1d426b to your computer and use it in GitHub Desktop.
remove duplicates from array
const names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl']
const count = names =>
names.reduce((a, b) =>
Object.assign(a, {[b]: (a[b] || 0) + 1}), {})
const duplicates = dict =>
Object.keys(dict).filter((a) => dict[a] > 1)
console.log(count(names)) // { Mike: 1, Matt: 1, Nancy: 2, Adam: 1, Jenny: 1, Carl: 1 }
console.log(duplicates(count(names))) // [ 'Nancy' ]
http://stackoverflow.com/questions/840781/easiest-way-to-find-duplicate-values-in-a-javascript-array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment