Hashing in Data Structure: Function, Techniques [With Examples] | upGrad blog (2024)

Introduction

Hashing is an important data structure designed to solve the problem of efficiently finding and storing data in an array. For example, if you have a list of 20000 numbers, and you have given a number to search in that list- you will scan each number in the list until you find a match.

Check out ourfree coursesto get an edge over the competition.

The hash function in the data structure verifies the file which has been imported from another source. A hash key for an item can be used to accelerate the process. It increases the efficiency of retrieval and optimises the search. This is how we can simply give hashing definition in data structure.

It requires a significant amount of your time to search in the entire list and locate that specific number. This manual process of scanning is not only time-consuming but inefficient too. With hashing in the data structure, you can narrow down the search and find the number within seconds.

You can also consider doing ourJava Bootcamp course from upGrad to upskill your career.

This blog will give you a deeper understanding of the hash method, hash tables, and linear probing with examples.

What is Hashing in Data Structure?

Hashing in the data structure is a technique of mapping a large chunk of data into small tables using a hashing function. It is also known as the message digest function. It is a technique that uniquely identifies a specific item from a collection of similar items.

Featured Program for you: Fullstack Development Bootcamp Course

It uses hash tables to store the data in an array format. Each value in the array has been assigned a unique index number. Hash tables use a technique to generate these unique index numbers for each value stored in an array format. This technique is called the hash technique.

You only need to find the index of the desired item, rather than finding the data. With indexing, you can quickly scan the entire list and retrieve the item you wish. Indexing also helps in inserting operations when you need to insert data at a specific location. No matter how big or small the table is, you can update and retrieve data within seconds.

The hash table is basically the array of elements and the hash techniques of search are performed on a part of the item i.e. key. Each key has been mapped to a number, the range remains from 0 to table size 1

Our learners also read: Data structures and Algorithms free course!

Types of hashing in data structure is a two-step process.

  1. The hash function converts the item into a small integer or hash value. This integer is used as an index to store the original data.
  2. It stores the data in a hash table. You can use a hash key to locate data quickly.

Examples of Hashing in Data Structure

The following are real-life examples of hashing in the data structure

  • In schools, the teacher assigns a unique roll number to each student. Later, the teacher uses that roll number to retrieve information about that student.
  • A library has an infinite number of books. The librarian assigns a unique number to each book. This unique number helps in identifying the position of the books on the bookshelf.

Checkout: Sorting in Data Structure

Hash Function

The hash function in a data structure maps the arbitrary size of data to fixed-sized data. It returns the following values: a small integer value (also known as hash value), hash codes, and hash sums. The hashing techniques in the data structure are very interesting, such as:

hash = hashfunc(key)

index = hash % array_size

The hash function must satisfy the following requirements:

  • A good hash function is easy to compute.
  • A good hash function never gets stuck in clustering and distributes keys evenly across the hash table.
  • A good hash function avoids collision when two elements or items get assigned to the same hash value.

One of the hashing techniques of using a hash function is used for data integrity. If using a hash function one change in a message will create a different hash.

The three characteristics of the hash function in the data structure are:

  1. Collision free
  2. Property to be hidden
  3. Puzzle friendly

Explore Our Software Development Free Courses

Fundamentals of Cloud Computing JavaScript Basics from the scratch Data Structures and Algorithms
Blockchain Technology React for Beginners Core Java Basics
Java Node.js for Beginners Advanced JavaScript

Hash Table

Hashing in data structure uses hash tables to store the key-value pairs. The hash table then uses the hash function to generate an index. Hashing uses this unique index to perform insert, update, and search operations.

It can be defined as a bucket where the data are stored in an array format. These data have their own index value. If the index values are known then the process of accessing the data is quicker.

In-Demand Software Development Skills

JavaScript Courses Core Java Courses Data Structures Courses
Node.js Courses SQL Courses Full stack development Courses
NFT Courses DevOps Courses Big Data Courses
React.js Courses Cyber Security Courses Cloud Computing Courses
Database Design Courses Python Courses Cryptocurrency Courses

upGrad’s Exclusive Software and Tech Webinar for you –

SAAS Business – What is So Different?

How does Hashing in Data Structure Works?

In hashing, the hashing function maps strings or numbers to a small integer value. Hash tables retrieve the item from the list using a hashing function. The objective of hashing technique is to distribute the data evenly across an array. Hashing assigns all the elements a unique key. The hash table uses this key to access the data in the list.

Hash table stores the data in a key-value pair. The key acts as an input to the hashing function. Hashing function then generates a unique index number for each value stored. The index number keeps the value that corresponds to that key. The hash function returns a small integer value as an output. The output of the hashing function is called the hash value.

Let us understand hashing in a data structure with an example. Imagine you need to store some items (arranged in a key-value pair) inside a hash table with 30 cells.

The values are: (3,21) (1,72) (40,36) (5,30) (11,44) (15,33) (18,12) (16,80) (38,99)

The hash table will look like the following:

Serial NumberKeyHashArray Index
133%30 = 33
211%30 = 11
34040%30 = 1010
455%30 = 55
51111%30 = 1111
61515%30 = 1515
71818%30 = 1818
81616%30 = 1616
93838%30 = 88

The process of taking any size of data and then converting that into smaller data value which can be named as hash value. This hash alue can be used in an index accessible in hash table. This process define hashing in data structure.

Also Read: Types of Data Structures in Python

Collision Resolution Techniques

Hashing in data structure falls into a collision if two keys are assigned the same index number in the hash table. The collision creates a problem because each index in a hash table is supposed to store only one value. Hashing in data structure uses several collision resolution techniques to manage the performance of a hash table.

It is a process of finding an alternate location. The collision resolution techniques can be named as-

  1. Open Hashing (Separate Chaining)
  2. Closed Hashing (Open Addressing)
  • Linear Probing
  • Quadratic Probing
  • Double Hashing

Linear Probing

Hashing in data structure results in an array index that is already occupied to store a value. In such a case, hashing performs a search operation and probes linearly for the next empty cell.

Linear probing in hash techniques is known to be the easiest way to resolve any collisions in hash tables. A sequential search can be performed to find any collision that occurred.

LearnSoftware Development Courses onlinefrom the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Explore our Popular Software Engineering Courses

Caltech CTME Cybersecurity Certificate Program
Full Stack Development Bootcamp PG Program in Blockchain
Executive PG Program in Full Stack Development
View All our Courses Below
Software Engineering Courses

Linear Probing Example

Imagine you have been asked to store some items inside a hash table of size 30. The items are already sorted in a key-value pair format. The values given are: (3,21) (1,72) (63,36) (5,30) (11,44) (15,33) (18,12) (16,80) (46,99).

The hash(n) is the index computed using a hash function and T is the table size. If slot index = ( hash(n) % T) is full, then we look for the next slot index by adding 1 ((hash(n) + 1) % T). If (hash(n) + 1) % T is also full, then we try (hash(n) + 2) % T. If (hash(n) + 2) % T is also full, then we try (hash(n) + 3) % T.

The hash table will look like the following:

Serial NumberKeyHashArray IndexArray Index after Linear Probing
133%30 = 333
211%30 = 111
36363%30 = 334
455%30 = 555
51111%30 = 111111
61515%30 = 151515
71818%30 = 181818
81616%30 = 161616
94646%30 = 81617

Double Hashing

The double hashing technique uses two hash functions. The second hash function comes into use when the first function causes a collision. It provides an offset index to store the value.

The formula for the double hashing technique is as follows:

(firstHash(key) + i * secondHash(key)) % sizeOfTable

Where i is the offset value. This offset value keeps incremented until it finds an empty slot.

For example, you have two hash functions: h1 and h2. You must perform the following steps to find an empty slot:

  1. Verify if hash1(key) is empty. If yes, then store the value on this slot.
  2. If hash1(key) is not empty, then find another slot using hash2(key).
  3. Verify if hash1(key) + hash2(key) is empty. If yes, then store the value on this slot.
  4. Keep incrementing the counter and repeat with hash1(key)+2hash2(key), hash1(key)+3hash2(key), and so on, until it finds an empty slot.

Also visit upGrad’s Degree Counselling page for all undergraduate and postgraduate programs.

Double Hashing Example

Imagine you need to store some items inside a hash table of size 20. The values given are: (16, 8, 63, 9, 27, 37, 48, 5, 69, 34, 1).

h1(n)=n%20

h2(n)=n%13

n h(n, i) = (h1 (n) + ih2(n)) mod 20

nh(n,i) = (h’(n) + i2) %20
16I = 0, h(n,0) = 16
8I = 0, h(n,0) = 8
63I = 0, h(n,0) = 3
9I = 0, h(n,0) = 9
27I = 0, h(n,0) = 7
37I = 0, h(n,0) = 17
48I = 0, h(n,0) = 8

I = 0, h(n,1) = 9

I = 0, h(n,2) = 12

5I = 0, h(n,0) = 5
69I = 0, h(n,0) = 9

I = 0, h(n,1) = 10

34I = 0, h(n,0) = 14
1I = 0, h(n,0) = 1

Read our Popular Articles related to Software Development

Why Learn to Code? How Learn to Code? How to Install Specific Version of NPM Package? Types of Inheritance in C++ What Should You Know?

Conclusion

Double hashing has a high computation cost, but it searches the next free slot faster than the linear probing method. The examples given in the article are only for explanatory purposes. You can modify the statements given above as per your requirements. In this blog, we learned about the concept of hashing in the data structure.

You can try out the example to strengthen your data structure knowledge. If you are curious to know more about data structure, check out the upGrad Executive PG Programme in Full Stack Development course. This course is designed for working professionals and offers rigorous training and job placement with top companies.

As a seasoned expert in data structures and hashing techniques, it's evident that the article provides a comprehensive overview of hashing in the context of data structures. The depth of knowledge is reflected in the precise explanations, real-life examples, and practical applications presented in the content. Let's break down the key concepts covered in the article:

  1. Hashing in Data Structure:

    • Hashing is a technique for mapping large data sets into smaller tables using a hashing function.
    • It involves using hash tables to store data in an array format, where each value is assigned a unique index number.
    • The hash function plays a crucial role in generating these unique index numbers.
  2. Hash Tables:

    • Hash tables are arrays that use hash functions to generate unique index numbers for efficient storage and retrieval of data.
    • The unique index numbers are crucial for quick searches and insertions, regardless of the table's size.
  3. Hash Function:

    • The hash function converts data into a small integer or hash value.
    • It must meet certain criteria, such as being easy to compute, avoiding clustering, and preventing collisions (two items mapped to the same hash value).
    • Hash functions contribute to data integrity, as a change in the input data should result in a different hash.
  4. Examples of Hashing:

    • Real-life examples include assigning unique roll numbers to students in schools and unique numbers to books in a library.
    • These examples illustrate how hashing simplifies the process of identifying and retrieving specific items.
  5. Collision Resolution Techniques:

    • Collisions occur when two keys are assigned the same index in the hash table.
    • Various collision resolution techniques are introduced, including open hashing (separate chaining) and closed hashing (open addressing).
    • Linear probing is highlighted as an effective method for resolving collisions.
  6. Linear Probing:

    • Linear probing is a collision resolution technique where the algorithm searches linearly for the next available slot in case of a collision.
    • It involves checking consecutive slots until an empty slot is found for insertion.
  7. Double Hashing:

    • Double hashing employs two hash functions to handle collisions.
    • The second hash function comes into play when the first one causes a collision, providing an offset index for storing the value.
  8. How Hashing Works:

    • Hashing involves mapping strings or numbers to small integer values using a hashing function.
    • Hash tables store data in key-value pairs, with the key used as input to the hashing function.
  9. Example Illustration:

    • A practical example is provided to demonstrate how hashing works in a hash table with a given size.
  10. Conclusion:

    • The article concludes by summarizing the hashing concepts discussed and emphasizes practical application for reinforcing data structure knowledge.

In conclusion, the article effectively caters to both beginners and those seeking a deeper understanding of hashing in data structures, making it a valuable resource for anyone looking to enhance their knowledge in this domain.

Hashing in Data Structure: Function, Techniques [With Examples] | upGrad blog (2024)

FAQs

What is hashing techniques in data structure with example? ›

Hashing involves applying a specific mathematical function to the input data, which produces a unique hash key that is typically of fixed length, regardless of the size of the input. The resulting hash key is essentially a digital fingerprint of the original data.

What are the examples of hashing function? ›

What are Examples of Common Cryptocurrency Hash Functions?
  • Message Digest (MD) Algorithm — MD2, MD4, MD5, and MD6. ...
  • Windows NTHash — Also known as a Unicode hash or NTLM, this hash is commonly used by Windows systems.
  • RACE Integrity Primitives Evaluation Message Digest (RIPEMD)
  • Whirlpool.
  • RSA.

What is hashing in data structure real life example? ›

There are many practical examples of hash tables used in every-day life. A popular example is in username-password databases. Every time someone signs up on a website using a username and password, that information must be stored somewhere for later retrieval.

What are the components of hashing? ›

Components of Hashing

Hash Function: The hash function receives the input key and returns the index of an element in an array called a hash table. The index is known as the hash index. Hash Table: Hash table is a data structure that maps keys to values using a special function called a hash function.

What is hash function technique in data structure? ›

In data structures, a hash function is used to calculate the hash value of a key, which is then used to store and retrieve the corresponding data. Hash functions are often used in conjunction with an array, where the hash value is used as an index in the array.

What are the different types of hashing technique explain? ›

Types of Hashing

There are many different types of hash algorithms such as RipeMD, Tiger, xxhash and more, but the most common type of hashing used for file integrity checks are MD5, SHA-2 and CRC32. MD5 - An MD5 hash function encodes a string of information and encodes it into a 128-bit fingerprint.

What is the most commonly used hash function? ›

The primary types of hash functions are:
  • Division Method.
  • Mid Square Method.
  • Folding Method.
  • Multiplication Method.
Apr 12, 2024

What is a hashing function for dummies? ›

A hashing algorithm is a mathematical function that takes an input (like a piece of text or a file) and converts it into a fixed-length string of characters, usually numbers or letters. This string called a "hash," is like a unique fingerprint for the input.

Which hashing technique is best in data structure? ›

Open hashing is a technique where every element of the hash table acts as a pointer to a linked list of elements containing similar hash values. Open hashing in data structure proves efficient in handling an unlimited amount of elements and is simple for the purpose of implementation.

Which hashing technique is best? ›

SHA-256 is one of the hashing algorithms that's part of the SHA-2 family (patented under a royalty-free U.S. patent 6829355). It's the most widely used and best hashing algorithm, often in conjunction with digital signatures, for: Authentication and encryption protocols, like TLS, SSL, SSH, and PGP.

What are five key properties of a hashing function? ›

The Characteristics of Cryptographic Hash Functions
  • It accepts a message of any length.
  • It produces a fixed-length message digest.
  • It is easy (and therefore fast) to compute the message digest for any given message.
  • The hash is irreversible – it is not possible to generate a message from its message digest.
Jul 17, 2018

What are the main features of a hash function? ›

Characteristics of a Good Hash Function. There are four main characteristics of a good hash function: 1) The hash value is fully determined by the data being hashed. 2) The hash function uses all the input data. 3) The hash function "uniformly" distributes the data across the entire set of possible hash values.

What are two functions of hashing? ›

A hash function is a function from some sort of input to a number. In particular, a hash function takes arbitrarily sized inputs to a fixed size output. It "shrinks" the input space to a predefined output range. There are two main uses for hash functions: data structures and security.

Where is hashing used in real life? ›

Image processing: Hashing is used in image processing applications, such as perceptual hashing, to detect and prevent image duplicates and modifications. File comparison: Hashing is used in file comparison algorithms, such as the MD5 and SHA-1 hash functions, to compare and verify the integrity of files.

What is an example of encryption and hashing? ›

For example, a piece of plain text (a password, for example) can be turned into a hashed value, making it very hard to decipher. Hashing is a one-way process that can't be directly reversed (as opposed to encryption, which can be decrypted if you have the appropriate decryption key).

What is hashing cyber security example? ›

For example, imagine you have a password "Password123." The hash function will transform it into a fixed-size hash value like "a8f2b78206f24b22f8befc8f1f0e4c16." Even if someone obtains the hash value, it would be extremely challenging for them to determine the original password without going through an extensive ...

Top Articles
Latest Posts
Article information

Author: Golda Nolan II

Last Updated:

Views: 6003

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Golda Nolan II

Birthday: 1998-05-14

Address: Suite 369 9754 Roberts Pines, West Benitaburgh, NM 69180-7958

Phone: +522993866487

Job: Sales Executive

Hobby: Worldbuilding, Shopping, Quilting, Cooking, Homebrewing, Leather crafting, Pet

Introduction: My name is Golda Nolan II, I am a thoughtful, clever, cute, jolly, brave, powerful, splendid person who loves writing and wants to share my knowledge and understanding with you.