DEV Community

Preeti yadav
Preeti yadav

Posted on

5 3 3 3 4

Mastering HashMaps: A Beginner-Friendly Guide

Why I Wrote This Blog 🤔

Honestly, I started studying Data Structures and Algorithms (DSA) because I wanted to get better at solving LeetCode problems. While learning, I came across HashMaps, but I struggled to find a simple and clear explanation. Most resources were either too complex or overloaded with jargon. 😵‍💫

So, I decided to write the blog I wish I had found—a super simple, beginner-friendly guide to HashMaps! If you’re someone who just wants a clear explanation (without feeling like you’re reading a textbook), this is for you. 😉

What Is a HashMap?

Imagine you have a magic diary 📖 where you can store information and find it instantly! Instead of flipping through pages, you just say a key (like a name), and it magically gives you the value (like a phone number). That’s exactly how a HashMap works!

🛠 How It Works:

You store data in key-value pairs.

A hash function decides where to put each key.
When you need a value, just give the key, and it quickly finds the value for you!

Real-Life Example:

Imagine a classroom where every student has a unique roll number and a name.

Roll Number (Key)      Name (Value)
101                    Rahul
102                    Priya
103                    Aman
Enter fullscreen mode Exit fullscreen mode

Now, if you want to know who has roll number 102, just ask the HashMap:

const students = new Map();
students.set(101, "Rahul");
students.set(102, "Priya");
students.set(103, "Aman");

console.log(students.get(102)); // Output: Priya
Enter fullscreen mode Exit fullscreen mode

Instead of searching one-by-one, HashMap jumps straight to the answer! 🚀

💻 How to Use HashMaps in JavaScript

1️⃣ Creating a HashMap

const studentMarks = new Map();
studentMarks.set("Rahul", 85);
studentMarks.set("Priya", 92);
studentMarks.set("Aman", 78);
Enter fullscreen mode Exit fullscreen mode

2️⃣ Accessing Values

console.log(studentMarks.get("Priya")); // Output: 92

Enter fullscreen mode Exit fullscreen mode

3️⃣ Checking If a Key Exists

console.log(studentMarks.has("Aman")); // Output: true

Enter fullscreen mode Exit fullscreen mode

4️⃣ Deleting a Key

studentMarks.delete("Aman");
console.log(studentMarks.has("Aman")); // Output: false
Enter fullscreen mode Exit fullscreen mode

5️⃣ Iterating Over a HashMap

for (let [key, value] of studentMarks) {
    console.log(`${key}: ${value}`);
}
Enter fullscreen mode Exit fullscreen mode

🏆 Practice Questions (Test Your Knowledge!)

Now it’s your turn! Try solving these challenges:

1️⃣ Create a HashMap where countries are the keys and their capitals are the values. Add at least 4 countries and display the capital of one country.

2️⃣ Write a program where you store employee IDs (keys) and their names (values) in a HashMap. Then, add a new employee and print the updated HashMap.

3️⃣ Suppose a HashMap stores student names as keys and their grades as values. Write a function to check if a student is in the HashMap and print their grade.

🚀 Drop your solutions in the comments! I’d love to check them out.

Final Thoughts

HashMaps are one of the most powerful data structures you’ll use in programming. Whether it’s handling user sessions, caching, or solving coding problems efficiently, HashMaps are everywhere! 🌍

If you’re also on a journey to master DSA and LeetCode, HashMaps will be one of your best friends! I hope this guide made HashMaps crystal clear for you. If it did, let me know in the comments! 💬 Also, follow me for more easy-to-understand programming content. 🚀

Happy coding! 😊

Top comments (0)