When Hashmaps Solve More Than Just Lookups


Hashmaps.
Everyone thinks they’re just glorified phonebooks for storing key: value
pairs.
But what if I told you they’ve been silently solving some of the trickiest DSA problems…
while arrays are still figuring out how not to get O(n) time for everything?
This article explores how Hashmaps do more than just .get()
.
From frquency tracking to LRU caches to saving your Wordle streak — they’re everywhere.
🧠 Quick Refresher: What is a Hashmap?
A Hashmap is a data structure that lets you:
Store data as
key → value
Look up a value in near-constant time
O(1)
Sound smarter in interviews when you say “hash collisions”
Common names you’ve probably seen:
JavaScript:
{}
orMap
Python:
dict
Java:
HashMap
C++:
unordered_map
🎯Problem 1: Frequency Count
Let’s say you’re given a string and asked:
“Which character appears the most?”
🤓 Old you: Loop over it multiple times, nested for
loops, pain.
🧠 New you with Hashmap:
const str = "hellooo";
const freq = {};
for (const char of str) {
freq[char] = (freq[char] || 0) + 1;
}
// Output: { h: 1, e: 1, l: 2, o: 3 }
You just:
Tracked occurrences in O(n)
Avoided nested loops
Made your interviewer nod approvingly
Real-World Twist: Used in Wordle, Spam filters, and anything with letter frequency analysis.
🚨Problem 2: Find First Duplicate in an Array
❌ Brute-force (aka, tears and regret):
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] === arr[j]) return arr[i];
}
}
Time: O(n²). Pain: Infinite.
✅ Hashmap Magic:
const seen = new Set();
for (const num of arr) {
if (seen.has(num)) return num;
seen.add(num);
}
O(n) time, O(n) space, 100% self-respect retention.
🚪 Problem 3: LRU Cache – Hashmap + Doubly Linked List
This is where things get spicy.
LRU Cache (Least Recently Used):
You want fast .get()
and .put()
operations while keeping the most recently used items — and evicting old ones.
Tools:
Hasmap → for O(1) access
Doubly linked list → for O(1) insert/delete order
High-Level Idea:
Hashmap gives you instant key access
DLL keep track of what was used recently
Oldest? Pop it from the tail
Newest? Add to the front
Real-World Twist: Used in browsers, operating systems, memory caches, and your mental RAM
(What did you eat for breakfast? Gone. But you remember that CSS bug from 2022.)
🧊 Problem 4: Sliding Window with Hashmap (Substrings, baby)
Find the length of the longest substring without repeating characters. Classic.
❌ Naive:
Check all substrings
Track each one's uniqueness
O(n²) or worse
✅ Hashmap + Sliding Window:
function lengthOfLongestSubstring(s) {
let map = new Map();
let left = 0, maxLen = 0;
for (let right = 0; right < s.length; right++) {
if (map.has(s[right])) {
left = Math.max(map.get(s[right]) + 1, left);
}
map.set(s[right], right);
maxLen = Math.max(maxLen, right - left + 1);
}
return maxLen;
}
Time: O(n)
Space: Hashmap
✨ Mood: Smooth
🔄 Bonus: Cycle Detection in Graphs
Sometimes Hashmaps are MVPs even in graph problems.
Use them to:
Track visited nodes
Memoize DFS results
Map keys to neighbors (adjacency list)
const graph = {
A: ["B"],
B: ["C"],
C: ["A"], // cycle!
};
const visited = new Set();
function hasCycle(node, path = new Set()) {
if (path.has(node)) return true;
if (visited.has(node)) return false;
visited.add(node);
path.add(node);
for (const neighbor of graph[node]) {
if (hasCycle(neighbor, path)) return true;
}
path.delete(node);
return false;
}
🧠 Recap: Hashmaps Are More Than .get()
Use Case | How Hashmaps Help |
Frequency Count | Track occurrences O(n) |
First Duplicate | Track seen values |
LRU Cache | Fast access + eviction logic |
Sliding Window | Remember char indices |
Graph Cycles | Track visited/memoized paths |
💬 Final Thoughts
You thought Hashmaps were just key-value stores?
Think again. They’re the quiet problem-solvers in:
Interview problems
Cache systems
Word games
And your next side project
So next time someone says,
“Oh this is easy, just use a hashmap…“
Ask them: “But do you know why?”
Want more articles where DSA gets clever?
This is just Day 1 of many.
Follow the blog: Code Like You Mean It – DSA Edition
Need a hashmap? You already have one. It’s called your brain.
Until next time — code like you mean it. ✌️
Subscribe to my newsletter
Read articles from Faiaz Khan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Faiaz Khan
Faiaz Khan
Hey! I'm Faiaz — a frontend developer who loves writing clean, efficient, and readable code (and sometimes slightly chaotic code, but only when debugging). This blog is my little corner of the internet where I share what I learn about React, JavaScript, modern web tools, and building better user experiences. When I'm not coding, I'm probably refactoring my to-do list or explaining closures to my cat. Thanks for stopping by — hope you find something useful or mildly entertaining here.