Hash Maps and Dictionaries

Say you have a list of words and you want to count how many times each one shows up. With just an array you would scan the whole thing again and again for every word. That gets slow fast. The fix is a hash map. A hash map is a structure that stores key-value pairs and lets you look up a value by its key in roughly constant time.

Every language ships one ready to use. You do not build it yourself. You just learn the method names. So in this tutorial we will use the built-in hash map in each language to count word frequencies and store key-value pairs.

🗺️ What Is a Hash Map?

Think of a real dictionary book. You want the meaning of “apple”? You do not read every page. You jump straight to the “A” section and find it. The word is the key. The meaning is the value.

A hash map works the same way:

  • A key is the thing you look something up by, like a word.
  • A value is the data attached to that key, like the count of that word.
  • You give the key and you get the value back fast.

The name changes by language but the idea is the same:

Language Built-in name
C++ unordered_map
Java HashMap
Python dict
JavaScript Map (or a plain object)
C none built-in (you write your own)

Note

C has no hash map in its standard library. So you either write one yourself or use an outside library. Because of that, our C example below builds a tiny fixed-size table by hand just to show the idea. The other four languages give you the real tool for free.

🔧 The Core Things You Always Do

Almost everything you do with a hash map is one of these operations. Learn these and you know the whole tool.

  • Put — store a value under a key. If the key is already there, it gets overwritten.
  • Get — read the value for a key.
  • Contains — check if a key exists before you use it.
  • Remove — delete a key and its value.
  • Iterate — walk through every key-value pair.

The method names differ a little per language, but each one does the same job. Here is the quick map:

Operation C++ Java Python JavaScript (Map)
Put m[k] = v m.put(k, v) m[k] = v m.set(k, v)
Get m[k] m.get(k) m[k] m.get(k)
Contains m.count(k) m.containsKey(k) k in m m.has(k)
Remove m.erase(k) m.remove(k) del m[k] m.delete(k)
Iterate for (auto& p : m) m.entrySet() m.items() for (const [k, v] of m)

🪜 Steps to Count Word Frequencies

We will do one clear task in every language. Take a list of words. Count how many times each word appears. Then read, check, remove, and print.

Here is the plan we follow in each language:

  1. Make an empty hash map.
  2. Go through each word in the list.
  3. For each word, add 1 to its count in the map. If the word is new, start it at 1.
  4. Get the count of one word and print it.
  5. Contains check: ask if some word is in the map.
  6. Remove one word from the map.
  7. Iterate over what is left and print every word with its count.

Now let us see this in all five languages.

💻 Word Count in Five Languages

Each tab below runs the same plan on the same word list: apple banana apple cherry banana apple.

⚡ How Fast Is It?

This is the part that makes a hash map special. Put, get, contains, and remove all run in O(1) average time. That means the work stays about the same whether the map holds ten keys or ten million. It does not slow down as it grows.

How? The map runs your key through a hash function. A hash function turns the key into a number that points straight to the right slot. There is no scanning, so you jump right to the spot.

Caution

“O(1) average” is the normal case, not a hard promise. Once in a while many keys land in the same slot. This is called a collision, and a bad run can drop to O(n) for that operation. For everyday code you can treat it as O(1).

Operation Average Worst case
Put O(1) O(n)
Get O(1) O(n)
Contains O(1) O(n)
Remove O(1) O(n)
Iterate all O(n) O(n)

⚠️ Common Mistakes to Avoid

A few traps catch almost everyone at first. Watch out for these.

  • Reading a missing key the wrong way. In Python, freq[word] throws an error if the word is not there. Use freq.get(word, 0) to get a safe default instead.
  • Forgetting hash maps have no order. C++ unordered_map and Java HashMap loop in no fixed order. Never count on the keys coming out the way you put them in.
  • Using a plain object in JS for non-string keys. A plain {} turns every key into a string. So the number 1 and the text "1" collide. Map keeps them apart.
  • Expecting m.count(k) in C++ to return the value. It returns 0 or 1, just whether the key exists. To read the value use m[k].
  • Modifying the map while you loop over it. Adding or deleting keys mid-loop can crash or skip items. Collect changes first, then apply them after the loop.

🧩 What You’ve Learned

✅ A hash map stores key-value pairs and looks them up by key fast.

✅ The built-in tool is unordered_map in C++, HashMap in Java, dict in Python, and Map in JavaScript. C has none built in.

✅ The core moves are put, get, contains, remove, and iterate.

✅ Counting word frequencies is the classic use, with freq[word]++ and friends.

✅ Put, get, contains, and remove are O(1) on average, which is why hash maps are everywhere.

✅ Hash maps keep no order, so never rely on key order when you loop.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What is the average time to get a value by its key in a hash map?

    Why: A hash function jumps straight to the right slot, so lookups are O(1) on average.

  2. 2

    Which built-in type is the hash map in Python?

    Why: Python's dict stores key-value pairs and is its built-in hash map.

  3. 3

    In Java, how do you check whether a key exists in a HashMap?

    Why: Java HashMap uses containsKey(k) to test for a key; has and count belong to other languages.

  4. 4

    Why should you avoid relying on key order when looping over a C++ unordered_map?

    Why: An unordered_map gives no ordering guarantee, so the loop order can look mixed up.

🚀 What’s Next?

Share & Connect