The single most useful data structure I reach for: the hash map. What it is, why lookups are instant, where I use it in real systems, and the catches to know about. No jargon.
If I had to keep only one data structure, it would be the hash map. Almost every system I build leans on it somewhere: find the police station for a pincode, look up a record by its ID, check “have I seen this one before.” All of those are the same question, “given X, give me Y instantly,” and the hash map is what makes the “instantly” part true.
This is the first post in a small series where I explain the basic building blocks of computing through things I have actually built. Let us start with the one I use the most.
A hash map stores pairs: a key and a value. You hand it a key, it hands back the value. It is exactly your phone’s contacts: you type a name (the key) and you get a number (the value). You do not scroll through every contact you have ever saved. You go straight to the one you asked for.
Other names for the same thing: a dictionary (Python), an object or Map (JavaScript), a HashMap (Java). Same structure, different labels.
Here is the part worth understanding, because it is genuinely clever and also simple.
Imagine you have a list of a hundred thousand records and you want the one with a particular ID. With a plain list, the computer has no choice but to start at the top and check each one until it finds a match. On average that is fifty thousand checks. We call that “order n,” meaning the work grows with the size of the list.
A hash map does something different. It runs the key through a small function called a hash function, which turns the key into a number. That number points directly at the slot where the value lives. So instead of searching, the computer calculates the address and goes straight there. One step, no matter how big the data is. We call that “order one,” or constant time.
The difference is not small. At a hundred thousand records, a list scan does tens of thousands of checks; the hash map does roughly one. That is the whole reason it feels instant.
# A lookup table: pincode -> police stationstation = { "110001": "Connaught Place PS", "400001": "Colaba PS",}
# Instant lookup. No scanning.print(station["110001"]) # Connaught Place PS
# Membership check, also instantprint("400001" in station) # TrueCompare that to a list, where finding the right entry means walking through it:
rows = [("110001", "Connaught Place PS"), ("400001", "Colaba PS")]match = Nonefor pincode, name in rows: # checks each item, one by one if pincode == "110001": match = name breakBoth work. The first one stays instant whether you have ten pincodes or ten lakh.
Once you have the eye for it, you see hash maps everywhere.
None of these are clever algorithms. They are all the same humble structure doing its one trick well.
A hash map is not magic, so here is what to keep in mind.
The hash map is the clearest example of a theme that runs through my work: the boring fundamental quietly powers the thing that looks clever. A semantic search, a dashboard, a dedup pipeline. Open them up and somewhere inside is a plain map turning “given X, find Y” into a single step.
Learn this one structure well and a surprising amount of everyday programming stops feeling like a search and starts feeling like a lookup.
Next in this series: binary search, and why sorted data is so powerful.