DEV Community

Nitinn S Kulkarni
Nitinn S Kulkarni

Posted on

๐Ÿงฎ Part 4: Hash Maps and Sets in Python โ€“ Lookups, Frequencies, and Fast Problem Solving

๐Ÿš€ Introduction

When it comes to efficient lookups, counting, grouping, and deduplication, nothing beats hash-based data structures like:

- dict (hash map) - set - defaultdict - Counter 
Enter fullscreen mode Exit fullscreen mode

These are the go-to tools in Python for solving problems in O(1) average time, and they're heavily used in interviews and real-world systems.

In this post, weโ€™ll explore:

โœ… Hash map and set fundamentals

โœ… Built-in Python tools: dict, set, defaultdict, and Counter

โœ… Interview patterns and real-world use cases

โœ… Clean, Pythonic solutions to classic problems

๐Ÿ”‘ 1๏ธโƒฃ Hash Maps (dict)

A hash map is a key-value store with O(1) average time complexity for insertions, lookups, and deletions.

 student_scores = {"Alice": 85, "Bob": 92} student_scores["Charlie"] = 78 print(student_scores["Bob"]) # 92  
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Real Uses:
- Caching results
- Counting occurrences
- Mapping relationships (e.g., graph edges)

๐Ÿงฎ 2๏ธโƒฃ Frequency Counting with Counter

 from collections import Counter s = "mississippi" freq = Counter(s) print(freq['s']) # 4  
Enter fullscreen mode Exit fullscreen mode

โœ… Super useful for:

- Anagrams - Top K frequent elements - Histogram problems 
Enter fullscreen mode Exit fullscreen mode

๐Ÿงฐ 3๏ธโƒฃ Grouping with defaultdict

 from collections import defaultdict grouped = defaultdict(list) words = ["eat", "tea", "tan", "ate", "nat", "bat"] for word in words: key = ''.join(sorted(word)) grouped[key].append(word) print(grouped.values()) # Output: [['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']]  
Enter fullscreen mode Exit fullscreen mode

โœ… Avoids key errors and automatically initializes containers

๐Ÿ”Ž 4๏ธโƒฃ Fast Lookups with Sets

 seen = set() seen.add(10) print(10 in seen) # True  
Enter fullscreen mode Exit fullscreen mode

Sets are unordered collections of unique items. Great for:

- Removing duplicates - Fast existence checks - Solving 2-sum-like problems efficiently 
Enter fullscreen mode Exit fullscreen mode

๐Ÿงช 5๏ธโƒฃ Common Interview Problems

โœ… Two Sum (Leetcode #1)

 def two_sum(nums, target): seen = {} for i, num in enumerate(nums): complement = target - num if complement in seen: return [seen[complement], i] seen[num] = i 
Enter fullscreen mode Exit fullscreen mode

โœ… Group Anagrams (Leetcode #49)

 from collections import defaultdict def group_anagrams(strs): groups = defaultdict(list) for word in strs: key = ''.join(sorted(word)) groups[key].append(word) return list(groups.values()) 
Enter fullscreen mode Exit fullscreen mode

โœ… Longest Substring Without Repeating Characters (Leetcode #3)

 def length_of_longest_substring(s): seen = set() left = 0 max_len = 0 for right in range(len(s)): while s[right] in seen: seen.remove(s[left]) left += 1 seen.add(s[right]) max_len = max(max_len, right - left + 1) return max_len 
Enter fullscreen mode Exit fullscreen mode

โœ… Top K Frequent Elements (Leetcode #347)

 from collections import Counter import heapq def top_k_frequent(nums, k): count = Counter(nums) return heapq.nlargest(k, count.keys(), key=count.get) 
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  6๏ธโƒฃ Hash Map Patterns You Must Know

Pattern Use Case
Counting with Counter Frequencies, modes, majority element
Index Mapping with dict Fast access, graph edges
Sliding Window + Hash Map Longest substring, minimum window substring
Set Membership Fast lookup, detect duplicates
Reverse Mapping Flip keys and values

๐Ÿงช 7๏ธโƒฃ Real-World Applications

Application Data Structure
DNS caching Hash map (cache domain/IP)
Spell-checking Set (fast word lookup)
Chat usernames Set (enforce uniqueness)
User login states Dict (user_id โ†’ token)
Count words in logs Counter or defaultdict(int)

โœ… Best Practices

โœ”๏ธ Use Counter for counting instead of manual loops โœ”๏ธ Use defaultdict to simplify grouping โœ”๏ธ Use sets when you only care about existence or uniqueness โœ”๏ธ Avoid using mutable types (like lists) as dict/set keys โŒ Don't use list.count() in loops โ€“ it's O(n) 
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Summary

โœ”๏ธ Hash maps and sets are essential tools for solving problems efficiently
โœ”๏ธ Python gives you dict, set, Counter, and defaultdict โ€“ power tools in your DSA arsenal
โœ”๏ธ Know the patterns: counting, grouping, lookup, reverse mapping
โœ”๏ธ Mastering these unlocks solutions to a ton of real-world and interview-style problems
๐Ÿ”œ Coming Up Next:

๐Ÿ‘‰ Part 5: Recursion and Backtracking โ€“ Exploring Trees, Grids, and Puzzle Solvers

Weโ€™ll cover:

1. How recursion works under the hood 2. Recursive patterns 3. Backtracking templates (Sudoku, permutations) 4. Transitioning from recursion to DP 
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ฌ Have a favorite Counter or defaultdict trick? Tried solving 2-sum with brute force before learning hash maps? Letโ€™s chat in the comments! ๐Ÿง โœจ

Top comments (0)