๐ Hash Maps & Sets
In the previous lesson, you learned how hash tables work internally. Now let's explore the data structures you'll actually use in your programs: HashMap (for key-value pairs) and HashSet (for unique collections). These are built into almost every programming language!
From Theory to Practice
You've seen how hash tables provide O(1) lookups. But in real programming, you rarely implement a hash table from scratch. Instead, you use:
๐ Built-in Hash-Based Structures
- Python:
dict(HashMap) andset(HashSet) - Java:
HashMapandHashSet - JavaScript:
MapandSet - C++:
unordered_mapandunordered_set
These are your everyday tools for fast lookups and unique collections!
HashMap vs HashSet
Let's understand when to use each:
Key Differences
| Feature | HashMap | HashSet |
|---|---|---|
| Stores | Key-Value pairs | Unique values only |
| Duplicates | Unique keys, values can repeat | No duplicates |
| Use Case | Frequency counting, lookups | Duplicate detection |
| Time | O(1) average | O(1) average |
๐ก Quick Decision Guide
Use HashMap when:
- You need to associate values with keys (like a dictionary)
- You're counting frequencies
- You need to look up related information
Use HashSet when:
- You only care about presence/absence
- You need to remove duplicates
- You're checking membership
See Two Sum in Action
Common Problem-Solving Techniques
Now let's explore five powerful techniques using HashMap and HashSet. These patterns solve many real-world programming challenges:
1. Finding Pairs: The Two Sum Problem
Problem: Find Two Numbers That Add Up to a Target
Given an array of numbers and a target, find two numbers that sum to the target.
def two_sum(nums, target):
seen = {} # HashMap: value โ index
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return []
Time: O(n), Space: O(n)
Key insight: Store what you've seen, check for complement!
2. Grouping Items: Anagrams
Problem: Group Words That Are Anagrams
Given a list of words, group together words that are anagrams (same letters, different order).
def group_anagrams(words):
groups = {} # sorted_word โ [anagrams]
for word in words:
key = ''.join(sorted(word))
if key not in groups:
groups[key] = []
groups[key].append(word)
return list(groups.values())
Example: ["eat", "tea", "ate"] โ same key "aet"
3. Counting Frequencies
Problem: Find First Non-Repeating Character
Given a string, find the first character that appears only once.
def first_non_repeating(s):
freq = {}
for char in s:
freq[char] = freq.get(char, 0) + 1
for char in s:
if freq[char] == 1:
return char
return None
Pattern: Count frequencies, then find first with count 1
4. Prefix Sum Technique
Problem: Count Subarrays with Sum K
Find how many contiguous subarrays sum to a target value K.
def subarray_sum_k(nums, k):
prefix_sum = 0
sum_count = {0: 1}
result = 0
for num in nums:
prefix_sum += num
if prefix_sum - k in sum_count:
result += sum_count[prefix_sum - k]
sum_count[prefix_sum] = sum_count.get(prefix_sum, 0) + 1
return result
Trick: Use HashMap to store prefix sums!
5. Detecting Duplicates
Problem: Check if Array Has Duplicates
Determine if an array contains any duplicate values.
def contains_duplicate(nums):
seen = set()
for num in nums:
if num in seen:
return True
seen.add(num)
return False
HashSet makes it O(n)! (vs O(nยฒ) brute force)
The Complete Code
Click Run to execute your code
When to Use These Techniques
๐ก Recognizing the Right Tool
Here's how to recognize when to use HashMap or HashSet:
- โ "Find pair that..." โ Two Sum pattern with HashMap
- โ "Group by..." โ HashMap with custom keys
- โ "Count how many..." โ HashMap for frequency counting
- โ "Check duplicates" โ HashSet for O(n) solution
- โ "Subarray sum" โ Prefix sum + HashMap
These techniques appear frequently in coding challenges and real-world programming!
Summary
What You've Learned
You now know how to use HashMap and HashSet to solve common programming problems:
- HashMap: Key-value pairs for lookups, counting, and associations
- HashSet: Unique collections for membership testing and duplicate removal
- Five techniques: Pairs, grouping, frequencies, prefix sums, duplicates
- Performance: All operations are O(1) average case
- Applications: These patterns solve many real programming challenges
What's Next?
You've learned the practical use of hash-based structures. Next up: Advanced Hashing โ techniques used in distributed systems and large-scale applications!
Enjoying these tutorials?