📖 Scenario: Imagine you have a big box of keys and you want to find the right key quickly to open a door. Without any order, you have to check each key one by one, which takes a lot of time. This is like searching for a value in a list. A hash map is like having a special box with labeled slots where each key goes directly to its own slot, so you can find it instantly.
🎯 Goal: You will create a simple example to show how a hash map helps find values quickly by using keys, compared to searching in a list.
📋 What You'll Learn
Create a list of keys and values representing items and their prices
Create a hash map (object) with the same items and prices
Write code to find the price of a specific item by searching the list
Write code to find the price of the same item using the hash map
Print both results to compare the process
💡 Why This Matters
🌍 Real World
Hash maps are used in apps and websites to quickly find user data, settings, or any information by a key like username or ID.
💼 Career
Understanding hash maps is important for software developers to write fast and efficient code for searching and storing data.
Progress0 / 4 steps
1
Create a list of items with prices
Create a variable called itemsList that holds an array of objects. Each object should have name and price properties with these exact entries: { name: 'apple', price: 100 }, { name: 'banana', price: 50 }, { name: 'orange', price: 70 }.
DSA Typescript
Hint
Use square brackets [] to create an array. Inside, add objects with name and price keys.
2
Create a hash map for items and prices
Create a variable called itemsMap that holds an object with keys as item names and values as their prices. Use these exact entries: 'apple': 100, 'banana': 50, 'orange': 70.
DSA Typescript
Hint
Use curly braces {} to create an object. Each key is the item name in quotes, followed by a colon and the price.
3
Find price by searching the list
Write code to find the price of 'banana' by searching itemsList. Use a for loop with variables item to check each object's name. When item.name is 'banana', save item.price to a variable called priceFromList.
DSA Typescript
Hint
Use a for...of loop to check each item. Use if to compare item.name with 'banana'. Assign item.price to priceFromList and use break to stop the loop.
4
Find price using the hash map and print both results
Get the price of 'banana' from itemsMap and save it to a variable called priceFromMap. Then print both priceFromList and priceFromMap using console.log.
DSA Typescript
Hint
Access the price from itemsMap using square brackets with the key 'banana'. Use console.log to print both prices on separate lines.
Practice
(1/5)
1. Why do we use a Hash Map instead of a simple list to store key-value pairs?
easy
A. Because it allows fast lookup of values by keys
B. Because it stores data in sorted order automatically
C. Because it uses less memory than a list
D. Because it can only store numbers
Solution
Step 1: Understand the problem with lists
Lists require checking each item one by one to find a value by key, which is slow for large data.
Step 2: How hash maps solve this
Hash maps use a special function to find the exact place of a key quickly, making lookup fast.
Final Answer:
Because it allows fast lookup of values by keys -> Option A
Quick Check:
Fast key lookup = Because it allows fast lookup of values by keys [OK]
Hint: Hash maps speed up key-based searches [OK]
Common Mistakes:
Thinking hash maps sort data automatically
Believing hash maps use less memory always
Assuming hash maps only store numbers
2. Which of the following is the correct way to create a hash map (Map) in TypeScript?
easy
A. const map = new Map();
B. const map = Map();
C. const map = new map();
D. const map = new Map(); string, number;
Solution
Step 1: Recall TypeScript Map syntax
In TypeScript, a Map is created using new Map<KeyType, ValueType>().
Step 2: Check each option
const map = new Map(); uses correct syntax with new Map<string, number>(). Others have syntax errors or wrong casing.
Final Answer:
const map = new Map<string, number>(); -> Option A
When setting a key that already exists, the value is updated to the new one.
Step 2: Trace the code
First 'apple' is set to 5, then updated to 10. So map.get('apple') returns 10.
Final Answer:
10 -> Option D
Quick Check:
Updated value for 'apple' = 10 [OK]
Hint: Last set value for a key overwrites previous [OK]
Common Mistakes:
Assuming first value remains
Thinking get returns undefined if key repeated
Expecting error on duplicate keys
4. Identify the error in this TypeScript code that tries to use a hash map:
const map = new Map();
map['key'] = 'value';
console.log(map.get('key'));
medium
A. map.get() cannot be used to retrieve values
B. Map constructor requires key and value types
C. Using bracket notation to add key-value pairs does not work with Map
D. Map keys must be numbers only
Solution
Step 1: Understand how to add entries to Map
Map entries must be added using map.set(key, value), not bracket notation.
Step 2: Analyze the code behavior
Using map['key'] = 'value' adds a property to the object, not a Map entry, so map.get('key') returns undefined.
Final Answer:
Using bracket notation to add key-value pairs does not work with Map -> Option C
Quick Check:
Use map.set() to add entries [OK]
Hint: Use map.set() to add entries, not bracket notation [OK]
Common Mistakes:
Adding entries with bracket notation
Expecting map.get() to find bracket-added keys
Thinking Map keys must be numbers
5. You want to count how many times each word appears in a list of words using a hash map in TypeScript. Which approach correctly solves this problem?
hard
A. Use a Set to store words and count size for each word
B. Use a Map, for each word check if it exists, then increment or set to 1
C. Use a Map with word as value and count as key
D. Use an array and push words, then count duplicates manually later
Solution
Step 1: Understand the counting problem
We need to store each word as a key and count as a value, so Map<string, number> fits well.
Step 2: How to update counts
For each word, check if it exists in the map; if yes, increment count, else set count to 1.
Step 3: Why other options fail
Use an array and push words, then count duplicates manually later is inefficient and manual. Use a Map with word as value and count as key swaps key and value wrongly. Use a Set to store words and count size for each word's Set only stores unique words without counts.
Final Answer:
Use a Map<string, number>, for each word check if it exists, then increment or set to 1 -> Option B
Quick Check:
Counting words = Map with word keys and number counts [OK]
Hint: Map keys = words, values = counts; increment or set 1 [OK]
Common Mistakes:
Using Set which can't count duplicates
Swapping key and value types in Map
Counting manually with arrays inefficiently
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.