Created
July 12, 2022 23:23
-
-
Save cazlu8/1fc820f35695dd1a561403d4ab8ef522 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
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