Skip to main content

Hash Maps & Sets: The Power of Constant-Time Lookup

Hashing is one of the most powerful tools in a programmer’s toolbox when solving problems that require fast lookups, frequency counting, duplicate detection, or efficient pair and range queries — especially when time constraints make brute-force solutions impractical.

Hashing lets us store and retrieve information in constant timeO(1) on average. Whether it's checking existence, counting frequency, or storing computed values for quick reference, hash maps (dict) and hash sets (set) are go-to tools.


Illustration: Frequency Counter & Lookup

At the end, the hash map is:

{
1: 2,
2: 2,
3: 1,
4: 1
}

We now know the frequency of every element — in just one pass!


Problem: Find the First Duplicate Number

Problem:
Given an array of integers, return the first number that appears more than once.

❌ Brute-Force Approach (Nested Loops)

def first_duplicate(arr):
for i in range(len(arr)):
for j in range(i + 1, len(arr)):
if arr[i] == arr[j]:
return arr[i]
return -1
  • Time Complexity: O(n²)

✅ Optimized Using a Hash Set

def first_duplicate(arr):
seen = set()
for num in arr:
if num in seen:
return num
seen.add(num)
return -1
  • Time Complexity: O(n)
  • Space Complexity: O(n)
  • We reduce the nested loop into a single pass using constant-time lookup.

Problem: Return True if there exist two numbers in the array that add up to a given target.

❌ Brute-Force Approach

def has_pair(arr, target):
for i in range(len(arr)):
for j in range(i + 1, len(arr)):
if arr[i] + arr[j] == target:
return True
return False
  • Time Complexity: O(n²)

Hashing to the Rescue

def has_pair(arr, target):
seen = set()
for num in arr:
complement = target - num
if complement in seen:
return True
seen.add(num)
return False
  • Time Complexity: O(n)

Real-World Applications

DomainUse Case
CompilersVariable usage tracking
Spell CheckersFast dictionary lookup
E-commerceProduct ID tracking, deduplication
Social MediaTag or mention counting
SecurityHashing passwords, detecting duplicates

Summary

If you're looping and comparing values — ask yourself:
Can I store something in a set or map to avoid this loop?

  • Hash maps and sets are perfect for:

    • Frequency counting
    • Fast lookups (e.g., membership tests)
    • Eliminating duplicate work
    • Reducing time complexity from O(n²) → O(n)
  • They're foundational in more complex techniques like:

    • Sliding window + hash map
    • Hash map + prefix sum
    • Caching results (memoization)

Hashing gives you speed through structure. It's not just a data structure — it's a paradigm shift in how you think about solving problems.