GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing javascript.info — Filter anagrams back to the lesson Filter anagramsimportance: 4 Anagrams are words that have the same number of same letters, but in different order. For instance:
Write a function For instance:
From every anagram group should remain only one word, no matter which one. To find all anagrams, let’s split every word to letters and sort them. When letter-sorted, all anagrams are same. For instance:
We’ll use the letter-sorted variants as map keys to store only one value per each key:
Letter-sorting is done by the chain of calls in the line For convenience let’s split it into multiple lines:
Two different words The next line put the word into the map:
If we ever meet a word the same letter-sorted form again, then it would overwrite the previous value with the same key in the map. So we’ll always have at maximum one word per letter-form. At the end Here we could also use a plain object instead of the That’s how the solution can look: |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |