Skip to content

Instantly share code, notes, and snippets.

@valerybugakov
Last active July 20, 2016 11:47
Show Gist options
  • Select an option

  • Save valerybugakov/8afa0f5d38922b25ed7f90e699c8ee17 to your computer and use it in GitHub Desktop.

Select an option

Save valerybugakov/8afa0f5d38922b25ed7f90e699c8ee17 to your computer and use it in GitHub Desktop.
Anagram checker
function getAnagrams(words) {
const normalize = word => word.toLowerCase().split('').sort().join('').trim()
const map = {}
let n = 0
const anagrams = words.reduce((res, word, i) => {
const normalized = normalize(word)
if (typeof map[normalized] === 'number') {
res[map[normalized]].push(word)
return res
}
map[normalized] = n
res[n] = [word]
n++
return res
}, [])
return anagrams.filter(arr => arr.length > 1)
}
/*
Anagram checker:
['lol', 'oll', 'react', 'actre', 'llo', 'boom'] =>
[
['lol', 'oll', 'llo']
['react', 'actre']
]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment