When to Use

Pattern triggers

Look for these signals in the problem:

  • "Find if element exists" with O(1)O(1) requirement: When repeated lookups are needed, hash maps avoid O(n)O(n) scans each time.
  • "Two Sum" style problems: Storing complements in a hash map finds pairs in O(n)O(n) instead of O(n2)O(n^2).
  • "Count frequency" of elements: Hash maps naturally aggregate counts as you iterate through data.
  • "Group by" some property: Use transformed keys (like sorted strings for anagrams) to group related elements.
  • "First/last occurrence" tracking: Store indices as values to remember positions of elements.