Skip to content

Instantly share code, notes, and snippets.

@xexi
Created July 31, 2017 08:22
Show Gist options
  • Save xexi/97d0dd65525ab0cfaeec21708ec283e0 to your computer and use it in GitHub Desktop.
Save xexi/97d0dd65525ab0cfaeec21708ec283e0 to your computer and use it in GitHub Desktop.
find duplicates word in 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' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment