Skip to content

Instantly share code, notes, and snippets.

@faytranevozter
Created October 19, 2022 14:49
Show Gist options
  • Save faytranevozter/cb9714685e4876278983884bc4f40f91 to your computer and use it in GitHub Desktop.
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
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