📖 Scenario: Imagine you run a small library. You want to quickly find the phone number of any member by their name. You start by writing down names and phone numbers on a paper list.But as the list grows, finding a phone number by scanning the list one by one takes longer and longer. You want a faster way to find phone numbers without searching the whole list.
🎯 Goal: Build a simple program that stores member names and phone numbers, then quickly finds a phone number by name using a hash map (dictionary).This will show why hash maps exist and how they solve the problem of slow searching.
📋 What You'll Learn
Create a dictionary to store member names and phone numbers
Add a variable to hold the member name to search
Use the dictionary to find the phone number for the given member name
Print the found phone number or a message if not found
💡 Why This Matters
🌍 Real World
Hash maps are used in many apps and systems to quickly find data by a key, like looking up phone numbers, usernames, or product prices.
💼 Career
Understanding hash maps is essential for software developers, data engineers, and anyone working with data storage and retrieval to write efficient programs.
Progress0 / 4 steps
1
Create the members dictionary
Create a dictionary called members with these exact entries: "Alice": "555-1234", "Bob": "555-5678", "Charlie": "555-8765"
DSA C#
Hint
Use Dictionary<string, string> and add the exact name-phone pairs inside curly braces.
2
Add the member name to search
Add a string variable called searchName and set it to "Bob"
DSA C#
Hint
Declare a string variable named searchName and assign it the value "Bob".
3
Find the phone number using the dictionary
Use members.TryGetValue(searchName, out string phoneNumber) to check if searchName exists in members and store the phone number in phoneNumber
DSA C#
Hint
Use TryGetValue with searchName and an out variable phoneNumber inside an if statement.
4
Print the phone number or not found message
Inside the if block, write Console.WriteLine($"Phone number: {phoneNumber}"). Inside the else block, write Console.WriteLine("Member not found")
DSA C#
Hint
Use Console.WriteLine with string interpolation to print the phone number inside the if block, and a simple message inside the else block.
Practice
(1/5)
1. Why do we use a hash map in programming? HashMap<K, V> stores key-value pairs. What problem does it solve?
easy
A. It stores data in a fixed-size array only.
B. It sorts data automatically in ascending order.
C. It compresses data to save memory.
D. It allows fast lookup of values by keys, avoiding slow searches.
Solution
Step 1: Understand the problem hash maps solve
Hash maps provide a way to find values quickly using keys without searching all data.
Step 2: Compare with other data structures
Unlike lists or arrays, hash maps avoid slow linear searches by using a hash function to jump directly to data.
Final Answer:
It allows fast lookup of values by keys, avoiding slow searches. -> Option D
Quick Check:
Fast key-based lookup = C [OK]
Hint: Hash maps speed up key-based searches [OK]
Common Mistakes:
Thinking hash maps sort data automatically
Confusing hash maps with arrays
Believing hash maps compress data
2. Which of the following is the correct way to declare a hash map in C# that maps strings to integers?
easy
A. HashMap map = new HashMap();
B. Dictionary map = new Dictionary();
C. Map map = new Map();
D. Hashtable map = new Hashtable();
Solution
Step 1: Identify the correct C# collection type for hash maps
In C#, the standard hash map is called Dictionary<K, V>.
Step 2: Check syntax correctness
Dictionary map = new Dictionary(); uses Dictionary with correct generic syntax and constructor call.
Final Answer:
Dictionary map = new Dictionary(); -> Option B
Quick Check:
C# hash map = Dictionary [OK]
Hint: Use Dictionary for hash maps in C# [OK]
Common Mistakes:
Using Java's HashMap class name in C#
Using Map which is not a C# type
Trying to use Hashtable with generics
3. What will be the output of the following C# code?
var map = new Dictionary<string, int>();
map["apple"] = 5;
map["banana"] = 3;
map["apple"] = 7;
Console.WriteLine(map["apple"]);