Skip to content

Instantly share code, notes, and snippets.

@octokatherine
Last active November 23, 2021 05:32
Show Gist options
  • Save octokatherine/b38faffbcb3a1345ed44fd1f9db2e977 to your computer and use it in GitHub Desktop.
Save octokatherine/b38faffbcb3a1345ed44fd1f9db2e977 to your computer and use it in GitHub Desktop.
function groupAnagrams(words) {
const groupedWords = words.reduce((obj, word) => {
const sortedWord = word.split('').sort().join('')
if (!obj[sortedWord]) {
obj[sortedWord] = []
}
obj[sortedWord].push(word)
return obj
}, {})
return Object.values(groupedWords)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment