Skip to content
Optimal Lab

DSA · interactive

Hash map: group anagrams in one pass

Anagrams use the same letters. Give every word a key that is equal for anagrams, then use a hash map from key to group. One lookup per word, no comparing every pair.

Input

  1. eat
  2. tea
  3. tan
  4. ate
  5. nat
  6. bat

Hash map · key → group

  • empty

An empty map. Press Play to read the words one at a time.

Key

Word 0 of 6
  1. 1

    Go through the list once, left to right.

    —

  2. 2

  3. 3

  4. 4

  5. 5

In production

Tools and technologies

Grouping by a computed key is how databases and data tools group rows, not just an interview trick.

Not tied to one language — use these names when you search docs or design a system.