Created
October 19, 2022 14:49
-
-
Save faytranevozter/cb9714685e4876278983884bc4f40f91 to your computer and use it in GitHub Desktop.
mengelompokkan sebuah array of strings menjadi kumpulan anagram. ps: hanya berlaku untuk jumlah huruf yang sama
This file contains 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
const solve = (words) => { | |
const result = {} | |
for (word of words) { | |
const key = word.split('').sort().join('') | |
if (typeof result[key] === 'undefined'){ | |
result[key] = [] | |
} | |
result[key].push(word) | |
} | |
return Object.values(result) | |
} | |
const input = ['cook', 'save', 'taste', 'aves', 'vase', 'state', 'map'] | |
const res = solve(input) | |
console.log(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment