Open In App

Hashing in Data Structure

Last Updated : 01 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Hashing is a fundamental data structure that efficiently stores and retrieves data in a way that allows for quick access. It involves mapping data to a specific index in a hash table using a hash function that enables fast retrieval of information based on its key. This method is commonly used in databases, caching systems, and various programming applications to optimize search and retrieval operations.

Data Structures – Hashing

What is Hashing in Data Structure?

Hashing is a technique used in data structures to store and retrieve data efficiently. It involves using a hash function to map data items to a fixed-size array which is called a hash table.

How it works:

  1. Hash Function: You provide your data items into the hash function.
  2. Hash Code: The hash function crunches the data and give a unique hash code.
  3. Hash Table: The hash code then points you to a specific location within the hash table.

Hash Table in Data Structure

A hash table is also known as a hash map. It is a data structure that stores key-value pairs. It uses a hash function to map keys to a fixed-size array, called a hash table. This allows in faster search, insertion, and deletion operations.

Hash Function

The hash function is a function that takes a key and returns an index into the hash table. The goal of a hash function is to distribute keys evenly across the hash table, minimizing collisions (when two keys map to the same index).

Common hash functions include:

  • Division Method: Key % Hash Table Size
  • Multiplication Method: (Key * Constant) % Hash Table Size
  • Universal Hashing: A family of hash functions designed to minimize collisions

What is a Hash Collision?

A hash collision occurs when two different keys map to the same index in a hash table. This can happen even with a good hash function, especially if the hash table is full or the keys are similar.

Causes of Hash Collisions:

  • Poor Hash Function: A hash function that does not distribute keys evenly across the hash table can lead to more collisions.
  • High Load Factor: A high load factor (ratio of keys to hash table size) increases the probability of collisions.
  • Similar Keys: Keys that are similar in value or structure are more likely to collide.

Collision Resolution Techniques

There are two types of collision resolution techniques:

  1. Open Addressing:
    • Linear Probing: Search for an empty slot sequentially
    • Quadratic Probing: Search for an empty slot using a quadratic function
  2. Closed Addressing:
    • Chaining: Store colliding keys in a linked list or binary search tree at each index
    • Cuckoo Hashing: Use multiple hash functions to distribute keys

Applications of Hashing

Hash tables are used in a wide variety of applications, including:

  • Databases: Storing and retrieving data based on unique keys
  • Caching: Storing frequently accessed data for faster retrieval
  • Symbol Tables: Mapping identifiers to their values in programming languages
  • Network Routing: Determining the best path for data packets

Basics of Hashing in Data Structure

Easy Problem on Hashing

Medium Problem on Hashing

Hard Problem on Hashing

Quick Links :

Recommended:



Similar Reads

Introduction to Universal Hashing in Data Structure
Universal hashing is a technique used in computer science and information theory for designing hash functions. It is a family of hash functions that can be efficiently computed by using a randomly selected hash function from a set of hash functions. The goal of universal hashing is to minimize the chance of collisions between distinct keys, which c
5 min read
Static Data Structure vs Dynamic Data Structure
Data structure is a way of storing and organizing data efficiently such that the required operations on them can be performed be efficient with respect to time as well as memory. Simply, Data Structure are used to reduce complexity (mostly the time complexity) of the code. Data structures can be two types : 1. Static Data Structure 2. Dynamic Data
4 min read
Encryption vs Encoding vs Hashing
Pre-Requisite: Encryption, Encoding, Hashing. Encryption, Encoding, and Hahsing are similar kinds of things and have little difference between them. They all are used to change the format of the data or data transformation for different purposes. We will discuss them separately. Let us first discuss the definition of all these three processes and t
4 min read
Practice Problems on Hashing
In this article, we will discuss the types of questions based on hashing. Before understanding this, you should have idea about hashing, hash function, open addressing and chaining techniques (see: Introduction, Separate chaining, Open addressing). These are some key points in hashing: The purpose of hashing is to achieve search, insert and delete
8 min read
Coalesced hashing
Coalesced hashing is a collision avoidance technique when there is a fixed sized data. It is a combination of both Separate chaining and Open addressing. It uses the concept of Open Addressing(linear probing) to find first empty place for colliding element from the bottom of the hash table and the concept of Separate Chaining to link the colliding
5 min read
Find majority element using Hashing
Given an array of size N, find the majority element. The majority element is the element that appears more than [Tex]\floor{\frac{n}{2}} [/Tex]times in the given array. Examples: Input: [3, 2, 3] Output: 3 Input: [2, 2, 1, 1, 1, 2, 2] Output: 2 The problem has been solved using 4 different methods in the previous post. In this post hashing based so
3 min read
Applications of Hashing
In this article, we will be discussing of applications of hashing. Introduction:Database indexing: Hashing is used to index and retrieve data efficiently in databases and other data storage systems.Password storage: Hashing is used to store passwords securely by applying a hash function to the password and storing the hashed result, rather than the
6 min read
Hashing in Java
In hashing there is a hash function that maps keys to some values. But these hashing function may lead to collision that is two or more keys are mapped to same value. Chain hashing avoids collision. The idea is to make each cell of hash table point to a linked list of records that have same hash function value. Let's create a hash function, such th
6 min read
Find the smallest positive number missing from an unsorted array : Hashing Implementation
Given an unsorted array with both positive and negative elements including 0. The task is to find the smallest positive number missing from the array in O(N) time. Examples: Input: arr[] = {-5, 2, 0, -1, -10, 15} Output: 1 Input: arr[] = {0, 1, 2, 3, 4, 5} Output: 6 Input: arr[] = {1, 1, 1, 0, -1, -2} Output: 2 We can use hashing to solve this prob
5 min read
Rearrange characters in a string such that no two adjacent are same using hashing
Given a string str with repeated characters, the task is to rearrange the characters in a string such that no two adjacent characters are the same. If it is possible then print Yes else print No. Examples: Input: str = "geeksforgeeks" Output: Yes "egeksforegeks" is one such arrangement. Input: str = "bbbbb" Output: No Approach: The idea is to store
5 min read
Article Tags :
Practice Tags :