Hashing

What Is the Main Use of Hashing?

The most common everyday use of hashing is fast lookup in hash tables. Hashing also helps with integrity checks, duplicate detection and data partitioning.

On this page
  1. The short answer
  2. How hashing speeds up lookups
  3. Other common uses
  4. Why hashing fits so many jobs
  5. Summary

The short answer

The most common everyday use of hashing is fast lookup in hash tables. A hash table uses a hash function to turn a key into a slot number, so the program can go straight to the right place instead of checking every item. That is the job behind many dictionaries, maps and sets in programming languages.

How hashing speeds up lookups

Suppose a program stores values in a table of 10 slots. A hash function might turn the key “apple” into a number and then take the remainder after dividing by 10. If the result is 7, the value goes into slot 7. To find it later, the program hashes “apple” again and checks slot 7. The numbers here are made up, but the steps are the same ones real hash tables use.

Two keys can land in the same slot. This is called a collision. Hash tables handle collisions either by keeping a short list in that slot, or by probing for the next free slot. Good hash functions and sensible table sizes keep collisions uncommon.

Other common uses

Hashing does more than lookups:

  • Checking data integrity. Comparing a file’s hash with a published value shows whether the file has changed.
  • Finding duplicates. Identical files or records produce identical hashes, so they are easy to spot.
  • Indexing. Databases and caches use hashing to find records quickly.
  • Spreading data across servers. Partitioning uses hashing to decide where data lives. The consistent hashing algorithm is one example.
  • Signing data. Digital signatures usually sign a hash of the data rather than the whole thing.
  • Storing passwords. This uses special password hashing functions, not the ordinary hash used in hash tables.

Each of these is a different job, so there is no single official answer to the main use. In many programming courses, the expected answer is hash tables.

Why hashing fits so many jobs

A good hash function turns any input into a fixed-size value, gives the same output for the same input, and spreads outputs fairly evenly. Those properties make it useful for lookups, comparisons and placing data.

Summary

The main everyday use of hashing is fast lookup through hash tables. Other common uses include checking integrity, spotting duplicates, indexing data and spreading data across servers.

The complete guide to hashing

How the Consistent Hashing Algorithm Works
Read the guide

Keep exploring

Guides from other topics

All topics →

Recursive Algorithm Examples

Recursive algorithm examples in Python: factorial, binary search, tree traversal, Tower of Hanoi, and merge sort, with base cases explained for each.

Recursion 7 min read