Skip to content

Instantly share code, notes, and snippets.

@fnh fnh/group-anagrams.js Secret
Last active Jun 13, 2018

What would you like to do?
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'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.