Skip to content

Instantly share code, notes, and snippets.

@rzachariah
Created December 28, 2019 22:29
Show Gist options
  • Save rzachariah/3748a1d93d2b2925cea8e5dda0fe4ff2 to your computer and use it in GitHub Desktop.
Save rzachariah/3748a1d93d2b2925cea8e5dda0fe4ff2 to your computer and use it in GitHub Desktop.
const cities = ['Tokyo', 'London', 'Rome', 'Donlon', 'Kyoto', 'Paris'];
const result = groupPermutations(cities);
console.log(result);
function groupPermutations(words) {
const map = words.reduce((acc, next) => {
sortedName = next.toLowerCase().split('').sort().join('');
var list = acc[sortedName];
if (list == null) {
list = [];
acc[sortedName] = list;
}
list.push(next);
return acc;
}, {});
return Object.values(map);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment