Bird
0
0
DSA Cprogramming~30 mins

Why Hash Map Exists and What Problem It Solves in DSA C - See It Work

Choose your learning style9 modes available
Why Hash Map Exists and What Problem It Solves
📖 Scenario: Imagine you have a big box of keys and you want to find the right key quickly to open a door. Searching one by one takes too long. A hash map is like a magic box that helps you find the right key fast.
🎯 Goal: You will create a simple program that stores and finds phone numbers by names using a hash map concept. This will show why hash maps exist and how they solve the problem of slow searching.
📋 What You'll Learn
Create an array of strings called names with these exact entries: "Alice", "Bob", "Charlie"
Create an array of integers called phone_numbers with these exact entries: 12345, 67890, 54321
Create an integer variable called size and set it to 3
Write a function called find_phone_number that takes char* name, char* names[], int phone_numbers[], and int size and returns the phone number for the given name or -1 if not found
Use a for loop with variable i to search through names inside find_phone_number
Print the phone number for "Bob" using find_phone_number
💡 Why This Matters
🌍 Real World
Hash maps help apps like phonebooks, contact lists, and databases find information fast without checking every item.
💼 Career
Understanding hash maps is key for software developers to write efficient code for searching and storing data.
Progress0 / 4 steps
1
Create the Data Setup
Create an array of strings called names with these exact entries: "Alice", "Bob", "Charlie". Also create an array of integers called phone_numbers with these exact entries: 12345, 67890, 54321.
DSA C
Hint

Use arrays to store the names and phone numbers exactly as shown.

2
Add Size Variable
Create an integer variable called size and set it to 3 to represent the number of entries.
DSA C
Hint

Use an integer variable named size and assign it the value 3.

3
Write the Search Function
Write a function called find_phone_number that takes char* name, char* names[], int phone_numbers[], and int size. Use a for loop with variable i to search through names. If name matches names[i], return phone_numbers[i]. If not found, return -1.
DSA C
Hint

Use strcmp to compare strings inside the loop.

4
Print the Phone Number for Bob
Use printf to print the phone number for "Bob" by calling find_phone_number("Bob", names, phone_numbers, size).
DSA C
Hint

Use printf("%d\n", ...) to print the phone number.