Skip to content

Instantly share code, notes, and snippets.

@ChrisDobby
Created November 24, 2021 18:35
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 ChrisDobby/82c2e15cd4915a0e8a105328d5e16765 to your computer and use it in GitHub Desktop.
Save ChrisDobby/82c2e15cd4915a0e8a105328d5e16765 to your computer and use it in GitHub Desktop.
function groupAnagrams(anagrams: string[]) {
const grouped: { [name: string]: string[] } = {};
for(const anagram of anagrams) {
const sorted = anagram.split('').sort().join('');
if(!grouped[sorted]) {
grouped[sorted] = [anagram];
} else {
grouped[sorted].push(anagram);
}
}
return Object.values(grouped).sort();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment