Created
December 28, 2019 22:29
-
-
Save rzachariah/3748a1d93d2b2925cea8e5dda0fe4ff2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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