Skip to content

Instantly share code, notes, and snippets.

@cazlu8
Created July 12, 2022 23:23
Show Gist options
  • Save cazlu8/1fc820f35695dd1a561403d4ab8ef522 to your computer and use it in GitHub Desktop.
Save cazlu8/1fc820f35695dd1a561403d4ab8ef522 to your computer and use it in GitHub Desktop.
var groupAnagrams = function(array) {
const result = array.reduce((acc, cur, index) => {
let word = cur.split(``).sort((a, b) => a.localeCompare(b)).join(``)
if(!acc[word]){
acc[word] = []
acc[word].push(array[index])
} else {
acc[word].push(array[index])
}
return acc
}, {})
return Object.values(result)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment