-
-
Save fnh/f8a2954c8de8ee0d08ff3515dad98bc5 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
function anagramNormalized(str) { | |
return str.toLocaleLowerCase().split("").sort().join(); | |
} | |
function isAnagram(a, b) { | |
return anagramNormalized(a) === anagramNormalized(b); | |
} | |
function group(toBeGrouped, grouped = []) { | |
if (!toBeGrouped.length) { | |
return grouped; | |
} else { | |
let nextGroup = toBeGrouped.filter(element => isAnagram(element, toBeGrouped[0])); | |
let nextRest = toBeGrouped.filter(element => !isAnagram(element, toBeGrouped[0])); | |
return group(nextRest, [...grouped, nextGroup]); | |
} | |
} | |
const list = ['Tokyo', 'London', 'Rome', 'Donlon', 'Kyoto', 'Paris']; | |
console.log(group(list)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment