We start with an array of words that we want to group by anagrams.
For each word, we sort its characters alphabetically to create a key.
This way, all anagrams will have the same key.
Notice how "eat" and "tea" both produce the same sorted key "aet".
The word "tan" gets sorted to "ant", which is a different key.
"ate" also sorts to "aet", making it an anagram of "eat" and "tea".
"nat" sorts to "ant", matching with "tan".
Finally, "bat" sorts to "abt", creating a new key.
We start building our hash map using the sorted keys.
The first entry is "aet" → [ "eat" ]
When we process "tea", we find the key "aet" already exists.
So we add "tea" to the existing list for "aet".
Next, we process "tan" and create a new entry with key "ant".
For "ate", we add it to the existing list for key "aet".
For "nat", we add it to the existing list for key "ant".
Finally, we add "bat" with its key "abt" to complete our hash map.
The values from our hash map give us the grouped anagrams!
Each group contains words that are anagrams of each other.