Skip to main content
LeetCode Prep
CHAPTER 04 Beginner

Hash Maps and Sets

Updated: May 18, 2026
5 min read

# CHAPTER 4

Hash Maps and Sets

1. Chapter Introduction

If you are completely stuck on an interview problem and don't know what to do, your first instinct should be: "Can I use a Hash Map?" Hash Maps (Dictionaries in Python, HashMaps in Java, Objects/Maps in JS) and Hash Sets are the most powerful tools in a developer's arsenal. They perform the magic trick of trading a little bit of memory (Space) to achieve lightning-fast O(1) Time lookups. This chapter explains how Hash Tables work under the hood and how to apply them to classic interview patterns.

2. What is a Hash Table?

An array requires you to know the exact numeric index to find data. A Hash Map allows you to look up data using a custom "Key" (like a string or a custom object). It stores data in Key-Value pairs.

*How it works (The Magic):*

  1. 1. You want to store {"John": 95}.
  1. 2. The key "John" is passed through a Hash Function.
  1. 3. The Hash Function converts the string "John" into an integer (e.g., 42).
  1. 4. The system stores the value 95 at index 42 in a hidden underlying array.
  1. 5. Later, when you ask for "John", it runs the hash function, instantly gets 42, and returns 95 in O(1) Time.

3. Hash Sets vs. Hash Maps

  • Hash Map: Stores Key -> Value pairs. Used for mapping relationships or counting frequencies (e.g., {"apple": 5, "banana": 2}).
  • Hash Set: Stores ONLY unique Keys (no values). Used purely to check for existence or remove duplicates. (e.g., {"apple", "banana"}). Checking if "apple" in my_set: takes O(1) Time.

4. Hash Collisions (The Interview Question)

*Interviewer:* "What happens if the hash function generates the same index 42 for both 'John' and 'Mike'?" *Answer:* This is a Hash Collision. The most common way to resolve it is Chaining. Instead of storing just the value 95 at index 42, the array stores a Linked List. Both "John: 95" and "Mike: 88" are added to the Linked List at index 42. When retrieving, the map hashes to 42, then traverses the short linked list to find the exact key.

5. Pattern 1: The "Two Sum" Problem

This is the most famous interview question in the world (LeetCode #1). *Prompt:* Given an array of integers nums and an integer target, return the indices of the two numbers that add up to target. *Brute Force:* Nested loops testing every pair. Time: O(N^2).

*Optimized (The Hash Map):* We iterate through the array. For every number, we calculate the difference = target - num. Have we seen this difference before? We use a Hash Map to keep track of numbers we've seen and their indices.

python
123456789101112
def twoSum(nums, target):
    # Map stores {number: index}
    seen = {} 
    
    for i, num in enumerate(nums):
        diff = target - num
        if diff in seen:
            return [seen[diff], i]
        seen[num] = i
        
    return []
# Time: O(N), Space: O(N)

*Why this is beautiful:* We reduced a quadratic O(N^2) problem down to a linear O(N) problem simply by trading O(N) Space for a Hash Map.

6. Pattern 2: Frequency Maps

Whenever a problem asks "Find the most frequent...", "Find the first non-repeating...", or "Are these two strings anagrams?", a Frequency Map is the solution.

*Example: First Unique Character in a String*

java
12345678910111213141516
public int firstUniqChar(String s) {
    HashMap<Character, Integer> count = new HashMap<>();
    
    // Build frequency map
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        count.put(c, count.getOrDefault(c, 0) + 1);
    }
    
    // Find the first character with frequency 1
    for (int i = 0; i < s.length(); i++) {
        if (count.get(s.charAt(i)) == 1) return i;
    }
    return -1;
}
// Time: O(N), Space: O(1) (since max 26 English letters)

7. Pattern 3: Finding Duplicates / Intersection

If you need to find elements that exist in *both* Array A and Array B (Intersection), or quickly remove duplicates from an array, use a Hash Set.
javascript
1234
// Remove duplicates from an array
let arr = [1, 2, 2, 3, 4, 4];
let uniqueSet = new Set(arr); // Set contains {1, 2, 3, 4}
let uniqueArr = Array.from(uniqueSet);

8. Mini Project: Group Anagrams

*Prompt:* Given an array of strings ["eat","tea","tan","ate","nat","bat"], group the anagrams together. *The Trick:* What do "eat", "tea", and "ate" have in common? If you sort them alphabetically, they all become "aet". *The Algorithm:*
  1. 1. Create a Hash Map: Map<String, List<String>>.
  1. 2. For each string, sort its characters to create a "key" ("aet").
  1. 3. Append the original string to the list located at that key in the map.
  1. 4. Return the values of the map.

9. Common Mistakes

  • Using Unhashable Types as Keys: In Python, you cannot use a list or a dictionary as a key in a dictionary because they are mutable (can change). You must use an immutable type like a String, Integer, or Tuple.
  • Forgetting Space Complexity: When you declare map = {}, you are using extra memory. You must state: "This takes O(N) extra space."

10. Best Practices

  • getOrDefault / .get(key, default): Always use these built-in methods when building frequency maps to elegantly handle keys that do not exist in the map yet, avoiding NullPointerException or KeyError.

11. Exercises

  1. 1. Trace the twoSum algorithm on paper for nums = [3, 2, 4], target = 6.
  1. 2. Explain how converting an Array to a Set affects Time and Space complexity.

12. MCQs

Question 1

What is the average Time Complexity for inserting and retrieving an item from a Hash Map?

Question 2

What is a "Hash Collision"?

Question 3

How is a Hash Collision typically resolved internally by data structures like Java's HashMap?

Question 4

What is the fundamental difference between a Hash Map and a Hash Set?

Question 5

In the classic "Two Sum" optimal O(N) solution, what are we trading to achieve linear time?

Question 6

When building a "Frequency Map" to count occurrences of characters in a string, what is the best practice for handling a character seen for the very first time?

Question 7

If you use a Hash Map to count the frequencies of characters in an English word (only lowercase letters 'a' to 'z'), what is the Space Complexity?

Question 8

Why can't you use an array (e.g., [1, 2, 3]) as a Key in a Python Dictionary?

Question 9

If an interviewer asks you to find the "Intersection" of two arrays (elements that exist in both), what is the optimal data structure to use?

Question 10

In the "Group Anagrams" problem, what is the clever "Key" used in the Hash Map to group the words "eat", "tea", and "ate" together?

14. Interview Questions

  • Q: "Design a Data Structure that supports insert, delete, and getRandomElement all in O(1) average time complexity." (LeetCode 380. Hint: You need to combine a Hash Map AND an Array).

15. FAQs

  • Q: In an interview, should I explain how the Hash Function works internally?
A: No, treat the Hash Map as a "black box" that gives you O(1) lookups, unless the interviewer explicitly asks you about collisions.

16. Summary

Hash Maps and Sets are the ultimate cheat code for optimizing algorithms. By trading O(N) Space, you eliminate nested loops and reduce O(N^2) time complexities down to O(N). Master the Two Sum pattern (storing elements as you iterate), Frequency counting, and using Sets to find intersections. Always remember that Keys must be immutable, and be prepared to explain Hash Collisions (Chaining) if prompted.

17. Next Chapter Recommendation

We know how to use extra memory (Hash Maps) to speed up Array problems. But what if the interviewer forbids us from using extra memory? We must achieve O(1) Space. In Chapter 5: Two Pointer Techniques, we will learn how to traverse arrays efficiently using multiple pointers.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·