Skip to content

Instantly share code, notes, and snippets.

@snahor
Created July 6, 2017 06:14
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 snahor/034ced946703891558860829b67b5189 to your computer and use it in GitHub Desktop.
Save snahor/034ced946703891558860829b67b5189 to your computer and use it in GitHub Desktop.
// 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