Introduction to Hashing

Say you have a class of students and you want to find one person’s marks. If they are sitting in a long row with no order, you start at the first chair and check each one until you reach the right person. That checking one by one is slow. Hashing is the trick that lets you jump straight to the right spot without checking everyone. Let us see how.

πŸ˜– The Pain First

Imagine you store names and phone numbers in a plain list. You want Riya’s number. The computer does not know where Riya is. So it starts at the top and reads each entry until it finds her.

If Riya is the last one, you checked every single entry. With a list of one million people, that is one million checks in the worst case. We call this O(n) time, which means the work grows with the number of items n.

That is the pain. Searching a plain list is slow because you may have to look at everything.

Now the question is simple. What if the computer could go straight to Riya’s spot in one move, without reading anyone else? That is exactly what hashing gives you.

πŸ“š What Is Hashing?

Hashing is a way to turn a key into a position in an array. A key is the thing you search by, like a name or an ID. The position is just an array index, like box number 7.

There is a small helper that does this turning. We call it a hash function. A hash function takes a key and gives back a number, and that number tells you which box to use.

So the flow is short:

  • You have a key, say "Riya".
  • The hash function reads the key and produces an index. Let us say it gives back box 4 for now.
  • You go to box 4 and store or read Riya’s value there.

Next time you want Riya, you run the same hash function. It gives back the same box 4 again, so you go straight there. That is just one move, and you never scan the whole list.

πŸ”‘ A Real-World Analogy

Think about lockers in a school. Each student gets a locker number. Arjun does not search every locker to find his bag. He remembers his number, walks to that locker, and opens it.

The locker number is like the index. The rule that says β€œArjun gets locker 12” is like the hash function. Because Arjun knows the rule, he reaches his locker in one move.

A book index works the same way. You do not read every page to find the word β€œloops”. You look up β€œloops” in the index, it tells you page 84, and you turn straight to page 84.

🧭 Key to Index, Step by Step

Here is the whole idea in one picture. The key goes into the hash function, and an index comes out. The number 4 below is just a sample to show the shape of the flow.

Key: Riya

Hash Function

Index (example): 4

Array box stores the value

Let us make the hash function real with a tiny example. Say our array has 5 boxes, numbered 0 to 4. One simple rule is this:

  • Add up the character codes of the letters in the key.
  • Divide by the array size and keep the remainder.
  • That remainder is the index.

The remainder trick uses the modulo operation, written as %. It always gives a number from 0 up to size minus 1, so the index always lands inside the array.

When you run this rule on a key, the work is the same small amount no matter how big the array is. That is why a lookup is about O(1) time, meaning the time stays nearly constant and does not grow with n.

βš™οΈ A Tiny Hashing Demo

Let us see this in real code. Most languages already give you a built-in map or dictionary that uses hashing inside. You put in a key and a value, then read it back by key. You never write the hash function yourself, because the language does it for you.

Below we store three students with their marks, then look up one student by name. Notice that we never scan a list. We just ask for the key directly.

Python’s dictionary is a hash map. You store a value under a key, then read it back by key in about one step.

hashing_demo.py
# A Python dict uses hashing inside to find each key fast
marks = {}
marks["Riya"] = 92
marks["Arjun"] = 85
marks["Alex"] = 78
# Look up by key directly, with no scanning of the whole list
print("Riya's marks:", marks["Riya"])
print("Arjun's marks:", marks["Arjun"])

The output of the above code will be:

Riya's marks: 92
Arjun's marks: 85

πŸͺ£ Two Keys, One Box: Collisions

There is one thing you should know now. Sometimes two different keys hash to the same index. Riya might land on box 2, and so might Arjun. That clash is called a collision.

A collision is not a bug. It happens because the array has fewer boxes than there are possible keys. Good hashing systems plan for it and have a way to keep both values in that spot.

You do not need to solve collisions today. Just remember they exist, and that the hash table handles them. We cover the actual methods in the hash table tutorial.

Note

A hash function should spread keys evenly across the boxes. The more even the spread, the fewer collisions, and the faster your lookups stay.

⏱️ Why It Is Fast

Let us compare the plain list with hashing side by side, so the speed difference is clear.

Operation Plain List Hashing
Search by key O(n) O(1) average
Insert O(1) at end O(1) average
Delete by key O(n) O(1) average

We say O(1) average and not always. In the rare case where many keys collide into one box, a lookup can slow down. With a good hash function that stays rare, so in real use hashing feels like one quick step.

⚠️ Common Mistakes To Avoid

A few things trip up beginners when they first meet hashing:

  • Thinking the index comes from the order you inserted keys. It does not. The index comes only from the key through the hash function.
  • Expecting keys to stay sorted. A hash map does not keep order. If you need sorted keys, that is a different structure.
  • Believing lookups are always O(1). They are O(1) on average. Heavy collisions can slow it down.
  • Trying to write your own hash function for real projects. Use the built-in map or dictionary, because it is already tuned.

🧩 What You’ve Learned

βœ… Searching a plain list is O(n) because you may check every item.

βœ… Hashing turns a key into an array index using a hash function, so you reach the spot in about O(1) time.

βœ… The same key always gives the same index, so storing and looking up both go straight to one box.

βœ… Built-in maps and dictionaries already use hashing inside, so you rarely write the hash function yourself.

βœ… Collisions happen when two keys land on the same box, and the hash table handles them for you.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What does a hash function do?

    Why: A hash function takes a key and produces an index that points to a box in the array.

  2. 2

    Why is searching a plain list slow?

    Why: Without a direct index, the search scans items one by one, so the worst case is O(n).

  3. 3

    What is a collision in hashing?

    Why: A collision is when two different keys hash to the same box, and the hash table handles it.

  4. 4

    What is the average time to look up a key with hashing?

    Why: Hashing jumps straight to the index, so an average lookup takes about constant O(1) time.

πŸš€ What’s Next?

Share & Connect