Created
July 6, 2017 06:14
-
-
Save snahor/034ced946703891558860829b67b5189 to your computer and use it in GitHub Desktop.
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
// count occurrences of the same combination of characters in an array of strings | |
// example: | |
// > console.log(count(['ab', 'ba'])) | |
// [2] | |
const xs = [ 'aba', 'aab', 'ccc', 'ddc', 'dcd', 'baa', 'dds', 'sdd', 'dcd', 'xyz' ]; | |
const count = xs => { | |
const map = xs.reduce((acc, x) => { | |
const key = Array.from(new Set(x)).sort().join(''); | |
return { | |
...acc, | |
[key]: (acc[key] || 0) + 1 | |
}; | |
}, {}); | |
return Object.values(map).sort().reverse(); | |
} | |
// > console.log(count(xs)) | |
// [3, 3, 2, 1, 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment