Last active
July 20, 2016 11:47
-
-
Save valerybugakov/8afa0f5d38922b25ed7f90e699c8ee17 to your computer and use it in GitHub Desktop.
Anagram checker
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
| 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