Last active
November 23, 2021 05:32
-
-
Save octokatherine/b38faffbcb3a1345ed44fd1f9db2e977 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
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