LeetCopilot Logo
LeetCopilot
LeetCode Pattern/Hash Map/Hash Map vs Hash Set: When to Use Each (Decision Guide)

Hash Map vs Hash Set: When to Use Each (Decision Guide)

LeetCopilot Team
Dec 22, 2025
6 min read
Hash MapHash SetDecision FrameworkInterview Prep
Stop defaulting to hash maps when sets would suffice. Learn the simple rule for choosing between maps and sets based on what you need to store.

You're solving "Contains Duplicate." You write:

python
seen = {}
for num in nums:
    if num in seen:
        return True
    seen[num] = True

It works. But you're using a hash map when a set would be cleaner, clearer, and more efficient.

This is a common beginner mistake: defaulting to hash maps when sets are sufficient.

This guide gives you the simple decision rule for choosing between hash maps and sets.

The Core Difference

Hash Set

Stores: Keys only (presence/absence)

python
seen = set()
seen.add(5)
5 in seen  # True

Use when: You only care if something exists.

Hash Map

Stores: Key-value pairs

python
seen = {}
seen[5] = "some value"
5 in seen  # True
seen[5]    # "some value"

Use when: You need associated data with each key.

The Decision Rule

code
Do you need to store anything besides the key?
├─ Yes → Hash map
└─ No → Hash set

That's it. Let's see examples.

When to Use Hash Set

Example 1: Contains Duplicate (LeetCode 217)

Problem: Check if array contains any duplicates.

Hash map (overkill):

python
def containsDuplicate(nums):
    seen = {}
    for num in nums:
        if num in seen:
            return True
        seen[num] = True  # Don't need this value!
    return False

Hash set (correct):

python
def containsDuplicate(nums):
    seen = set()
    for num in nums:
        if num in seen:
            return True
        seen.add(num)
    return False

# Or even simpler
def containsDuplicate(nums):
    return len(nums) != len(set(nums))

Why set is better:

  • Clearer intent: "I only care about presence"
  • Simpler API: add() vs [key] = value
  • Slightly more memory efficient

Example 2: Intersection of Two Arrays (LeetCode 349)

Problem: Find unique elements that appear in both arrays.

Hash set:

python
def intersection(nums1, nums2):
    set1 = set(nums1)
    set2 = set(nums2)
    return list(set1 & set2)  # Set intersection

# Or
def intersection(nums1, nums2):
    set1 = set(nums1)
    return [num for num in set2 if num in set1]

Why set is better:

  • Only need presence, not counts
  • Set operations (&, |, -) are natural
  • No need for values

Example 3: Happy Number (LeetCode 202)

Problem: Detect cycle in number transformation.

Hash set:

python
def isHappy(n):
    seen = set()
    
    while n != 1:
        if n in seen:
            return False  # Cycle detected
        seen.add(n)
        n = sum(int(d)**2 for d in str(n))
    
    return True

Why set is better:

  • Only checking if we've seen this number before
  • Don't need to store any associated data

When to Use Hash Map

Example 1: Two Sum (LeetCode 1)

Problem: Find two numbers that sum to target, return indices.

Hash map (need indices):

python
def twoSum(nums, target):
    seen = {}  # Can't use set - need to store indices!
    
    for i, num in enumerate(nums):
        complement = target - num
        if complement in seen:
            return [seen[complement], i]
        seen[num] = i  # Store index as value
    
    return []

Why map is needed:

  • Need to store index (value) for each number (key)
  • Set can't store this association

Example 2: First Unique Character (LeetCode 387)

Problem: Find first non-repeating character.

Hash map (need counts):

python
from collections import Counter

def firstUniqChar(s):
    freq = Counter(s)  # Map: char → count
    
    for i, char in enumerate(s):
        if freq[char] == 1:
            return i
    
    return -1

Why map is needed:

  • Need to store count (value) for each character (key)
  • Set can only tell us presence, not frequency

Example 3: Group Anagrams (LeetCode 49)

Problem: Group words that are anagrams.

Hash map (need to group):

python
from collections import defaultdict

def groupAnagrams(strs):
    groups = defaultdict(list)  # Map: key → list of words
    
    for word in strs:
        key = ''.join(sorted(word))
        groups[key].append(word)  # Store list as value
    
    return list(groups.values())

Why map is needed:

  • Need to store list of words (value) for each pattern (key)
  • Set can't store these associations

Comparison Table

AspectHash SetHash Map
StoresKeys onlyKey-value pairs
PurposePresence checkAssociated data
APIadd, remove, inget, set, in
MemorySlightly lessSlightly more
Use caseMembership, dedupNeed values

Common Patterns

Pattern 1: Deduplication → Set

python
# Remove duplicates
unique = list(set(nums))

# Count unique
count = len(set(nums))

Pattern 2: Membership testing → Set

python
# Check if element exists
allowed = set([1, 2, 3, 4, 5])
if num in allowed:
    # ...

Pattern 3: Cycle detection → Set

python
# Detect if we've seen this state before
seen = set()
while state not in seen:
    seen.add(state)
    state = transform(state)

Pattern 4: Frequency counting → Map

python
# Count occurrences
from collections import Counter
freq = Counter(nums)

Pattern 5: Index tracking → Map

python
# Store positions
index_map = {}
for i, num in enumerate(nums):
    index_map[num] = i

Pattern 6: Grouping → Map

python
# Group by property
groups = defaultdict(list)
for item in items:
    key = compute_key(item)
    groups[key].append(item)

Common Mistakes

Mistake 1: Using map when set suffices

Wrong:

python
# Contains duplicate with map
seen = {}
for num in nums:
    if num in seen:
        return True
    seen[num] = True  # Unnecessary value

Correct:

python
# Use set
seen = set()
for num in nums:
    if num in seen:
        return True
    seen.add(num)

Mistake 2: Using set when you need values

Wrong:

python
# Two Sum with set - can't store indices!
seen = set()
for i, num in enumerate(nums):
    if target - num in seen:
        return [?, i]  # Where's the other index?
    seen.add(num)

Correct:

python
# Use map to store indices
seen = {}
for i, num in enumerate(nums):
    if target - num in seen:
        return [seen[target - num], i]
    seen[num] = i

Mistake 3: Not using set operations

Verbose:

python
# Intersection with loops
result = []
set1 = set(nums1)
for num in nums2:
    if num in set1:
        result.append(num)

Pythonic:

python
# Use set operations
result = list(set(nums1) & set(nums2))

Quick Reference

Use Set when you need:

  • ✅ Check if element exists
  • ✅ Remove duplicates
  • ✅ Detect cycles
  • ✅ Membership testing
  • ✅ Set operations (union, intersection, difference)

Use Map when you need:

  • ✅ Count frequencies
  • ✅ Store indices/positions
  • ✅ Group items
  • ✅ Associate data with keys
  • ✅ Track metadata per element

Practice Problems

Use Set

  1. Contains Duplicate (#217) - Presence only
  2. Happy Number (#202) - Cycle detection
  3. Intersection of Two Arrays (#349) - Set intersection
  4. Longest Consecutive Sequence (#128) - Membership

Use Map

  1. Two Sum (#1) - Need indices
  2. Group Anagrams (#49) - Need to group
  3. Top K Frequent Elements (#347) - Need counts
  4. First Unique Character (#387) - Need frequency

Could use either (but set is cleaner)

  1. Valid Sudoku (#36) - Presence check
  2. Word Pattern (#290) - Bidirectional mapping (need map)

Summary

The choice between hash map and set is simple.

The rule:

code
Need associated values? → Hash map
Only need presence? → Hash set

Hash set:

  • Clearer intent
  • Simpler API
  • Slightly more efficient
  • Use for: presence, dedup, cycles

Hash map:

  • Stores key-value pairs
  • More flexible
  • Use for: counts, indices, grouping

When in doubt: Start with set. If you realize you need values, upgrade to map.

Next steps:

Choose the right tool. Keep it simple.

Want to Practice LeetCode Smarter?

LeetCopilot is a free browser extension that enhances your LeetCode practice with AI-powered hints, personalized study notes, and realistic mock interviews — all designed to accelerate your coding interview preparation.

Also compatible with Edge, Brave, and Opera

Related Tutorials