Skip to main content

What is the idea behind hashing?

The idea behind hashing is to use a special function (a hash function) to quickly turn arbitrary data (for example, a string or an object) into a fixed number, a hash.

This number is used as an address for storing or looking up data in a structure, for example in a hash table.

Main goal

Cut down search time: instead of scanning every element, we compute right away where exactly the needed object should be.

Key properties of hashing

  1. Fast computation, the hash must be computed quickly.
  2. Even distribution, different keys should produce different (or at least evenly distributed) hashes.
  3. Determinism, the same key always produces the same hash.

Example

If we need to store users by name:

javascript
hash("Alice")42 hash("Bob")17 hash("Maria")93

Each name immediately goes to "its own" slot in the table.

In other words, hashing turns search into computation: instead of a long comparison of every element, we simply compute a number and go to the address.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.