Created
July 31, 2017 08:22
-
-
Save xexi/97d0dd65525ab0cfaeec21708ec283e0 to your computer and use it in GitHub Desktop.
find duplicates word in array
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 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