What Are Different Hash Functions in Data Structure?


Angela Bailey

In the world of data structures, hash functions play a crucial role. A hash function is a mathematical function that takes an input (or key) and returns a fixed-size string of characters, which is typically a numeric value.

This output is known as the hash value or hash code. Hash functions are widely used in computer science for various purposes, including data storage, data retrieval, and data encryption.

Why do we need hash functions?

Hash functions are primarily used for efficient data storage and retrieval. They help in organizing large amounts of data by mapping them to fixed-length values or indices. These indices can be easily used to access and retrieve the corresponding data in an efficient manner.

Properties of a good hash function:

A good hash function should possess certain properties to ensure efficient performance and avoid collisions:

  • Uniformity: The distribution of hash values should be as uniform as possible to minimize collisions. In other words, each input should have an equal chance of producing any possible hash value.
  • Determinism: The same input should always produce the same output.
  • Efficiency: The computation of the hash value should be fast and require minimal resources.
  • Minimization of collisions: Collisions occur when two different inputs produce the same hash value. Although it is impossible to completely eliminate collisions, a good hash function should aim to minimize them.

Types of Hash Functions:

In the realm of data structures, several types of hash functions are commonly used. Let’s explore some of them:

1. Division Method

The division method is one of the simplest hash functions. It involves dividing the key by the table size and using the remainder as the hash value. For example, if the table size is 10 and the key is 25, the hash value would be 5 (25 % 10).

2. Multiplication Method

The multiplication method involves multiplying the key by a constant fraction and extracting the fractional part of the product. The resulting fractional part is then multiplied by the table size to obtain the hash value.

3. Folding Method

In the folding method, the key is divided into equal-sized parts (usually of fixed length). These parts are then summed up to obtain a hash value. This method is commonly used when dealing with large keys.

4. Mid-Square Method

The mid-square method involves squaring the key and extracting a portion of its middle digits as the hash value. The number of middle digits extracted depends on the desired hash value size.

5. Universal Hashing

Universal hashing involves randomly selecting a hash function from a family of functions for each insertion or retrieval operation. This randomness helps in minimizing collisions and provides better performance in practice.


Hash functions are an essential concept in data structures, enabling efficient storage, retrieval, and security of data. Understanding different types of hash functions can aid in designing robust data structures that offer optimal performance and minimize collisions.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy