Access free live classes and tests on the app
Download
+
Unacademy
  • Goals
    • AFCAT
    • AP EAMCET
    • Bank Exam
    • BPSC
    • CA Foundation
    • CAPF
    • CAT
    • CBSE Class 11
    • CBSE Class 12
    • CDS
    • CLAT
    • CSIR UGC
    • GATE
    • IIT JAM
    • JEE
    • Karnataka CET
    • Karnataka PSC
    • Kerala PSC
    • MHT CET
    • MPPSC
    • NDA
    • NEET PG
    • NEET UG
    • NTA UGC
    • Railway Exam
    • SSC
    • TS EAMCET
    • UPSC
    • WBPSC
    • CFA
Login Join for Free
avtar
  • ProfileProfile
  • Settings Settings
  • Refer your friendsRefer your friends
  • Sign outSign out
  • Terms & conditions
  • •
  • Privacy policy
  • About
  • •
  • Careers
  • •
  • Blog

© 2023 Sorting Hat Technologies Pvt Ltd

NTA UGC NET 2023 » NTA Study Materials » Computer Science » Hash Table
doubtsolving_ntaugc

Hash Table

This article includes the study material notes on the Types of hash table, advantages of hash table, hash table-data structure and algorithms, etc.

Table of Content
  •  

There are different forms of tables that assist us in getting information extremely efficiently. The ideal hash table is just an array of a fixed size; the size is determined by the application in which it will be used. The hash table includes key values with links to the related records. The essential principle behind a hash table is to enter a key value into a hash table location; the location will be calculated from the key value itself. Address calculation indexing, or hashing, refers to the one-to-one correlation between a key value and an index in a hash table. We will cover hashing algorithms and data structure in this part.

Hash Table

A hash table is a data structure that stores data associatively. Data is kept in an array format in a hash table, with each data item having its own unique index value. If we know the index of the needed data, we can get to it quickly. As a result, it becomes a data structure in which insertion and search operations are extremely quick, regardless of data size. Hash Table employs an array as a storage medium and uses the hash technique to produce an index where an element is to be put or to be located. 

Hashing

Hashing is a technique for converting a set of key values into a set of array indexes. This is frequently represented by a shorter, fixed-length value or key that reflects the original string and makes it simpler to discover or use it. The most common use for hashing is the creation of hash tables.

Linear Probing

The hashing algorithm utilises an array index that has previously been used. In such a circumstance, we may search for the next empty spot in the array by searching into the following cell till we discover an empty cell. This method is known as linear probing.

Basic Operations

The following are the core primary operations of a hash table.

Search – In a hash table, search for an element.

Insert – Insert an element into a hash table.

Delete – removes an element from a hash table.

DataItem 

Define a data item that contains some data and a key that will be used to search the hash table.

struct DataItem { 

     int data; 

     int key;

 };

Hash Method

Define a hashing technique for computing the hash code of the data item’s key.

int hashCode(int key){ 

    return key % SIZE;

 } 

Search Operation

Whenever an element has to be found, compute the hash code of the provided key and use that hashcode as an index in the array to find the element. If the element is not located at the computed hash code, use linear probing to obtain it ahead. 

struct DataItem *search(int key){

    //get the hash 

    int hashIndex = hashCode(key); 

    //move in array until an empty 

    while(hashArray[hashIndex] != NULL){ 

         if(hashArray[hashIndex]->key == key) 

            return hashArray[hashIndex]; 

         //go to next cell

        ++hashIndex; 

        //wrap around the table 

       hashIndex %= SIZE;

   } 

     return NULL; 

} 

Insert Operation

Whenever an element is to be introduced, compute the hash code of the provided key and use that hashcode as an index in the array to get the index. Use linear probing for empty location if an element is discovered at the calculated hash code.

void insert(int key,int data){ 

     struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));

     item->data = data;

     item->key = key; 

     //get the hash 

     int hashIndex = hashCode(key);

    //move in array until an empty or deleted cell 

    while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1){ 

          //go to next cell 

         ++hashIndex;

         //wrap around the table

         hashIndex %= SIZE; 

   } 

   hashArray[hashIndex] = item; 

} 

Delete Operation

When an element needs to be removed, compute the hash code of the key supplied and identify the index using that hashcode as the index in the array. If an element is not located at the computed hash code, use linear probing to advance it. When detected, place a fake item there to keep the hashtable’s performance unchanged.

struct DataItem* delete(struct DataItem* item){ 

     int key = item->key;

     //get the hash 

     int hashIndex = hashCode(key); 

    //move in array until an empty

    while(hashArray[hashIndex] !=NULL){ 

         if(hashArray[hashIndex]->key == key){ 

            struct DataItem* temp = hashArray[hashIndex];

            //assign a dummy item at deleted position 

          hashArray[hashIndex] = dummyItem; 

          return temp;

      }

      //go to next cell

     ++hashIndex; 

         //wrap around the table 

     hashIndex %= SIZE; 

  } 

  return NULL;

}

Algorithm

Hashing-based search algorithms are divided into two components. The first step is to generate a hash function that converts the search key to an array index. Various keys should ideally correspond to different indices. Because this ideal is typically out of reach, we must accept the chance that two or more distinct keys hash to the same array index. As a result, the second component of a hashing search is a collision-resolution procedure that handles this issue. Collision Resolution

Separate Chaining

Separate chaining entails creating a linked list with a key-value pair for each search array index. The objects that have collided are connected together in a single linked list, which may be browsed to retrieve the item with a unique search key. Collision resolution by chaining with a linked list is a typical way of implementing hash tables.

Open Addressing

Another method used to resolve collisions in hash tables is called open addressing. Here, rather than building secondary data structures, we resolve collisions by looking elsewhere in the table. Specifically, we have a sequence of hash functions 〈h0, h1, h2,…….,hm-1〉, such that for any item x, the probe sequence is a permutation of 〈0, 1, 2, . . . , m − 1〉. In other words, different hash functions in the sequence always map x to different locations in the hash table. We search for x using the following algorithm, which returns the array index i if T[i] = x, ‘absent’ if x is not in the table, but there is an empty slot, and ‘full’ if x is not in the table and there are no empty slots.

The algorithm for inserting a new item into the table is similar; only the second-to-last line is changed to T[hi (x)] ← x. Notice that for an open-addressed hash table, the load factor is never bigger than 1. Just as with chaining, we’d like to pretend that the sequence of hash values is truly random for analysis purposes. Specifically, most open-address hashing analysis uses the following assumption, which is impossible to enforce in practice but leads to reasonably predictive results for most applications.

Conclusion

Thus it is concluded that a hash table is a data structure that stores data associatively. The hash table includes key values with links to the related records. The essential principle behind a hash table is that we must enter a key value into a hash table location; the location will be calculated from the key value itself. Address calculation indexing, or hashing, refers to the one-to-one correlation between a key value and an index in a hash table.

faq

Frequently asked questions

Get answers to the most common queries related to the NTA Examination Preparation.

Hashtable employs what data structure?

Ans. A hash table, often known as a hash map, is a data structure in computing that implements a set abstract data type. A hash table employs a has...Read full

What is the hash table algorithm?

Ans. A hash function is a function that turns keys into array indices. The second component of a hashing algorithm is collision resolution.

Define Hash Table.

Ans. A hash table is a data structure that stores data associatively. Data is kept in an array format in a ha...Read full

Ans. A hash table, often known as a hash map, is a data structure in computing that implements a set abstract data type. A hash table employs a hash function to generate an index, also known as a hash code, into an array of buckets or slots from which the necessary item may be extracted.

Ans. A hash function is a function that turns keys into array indices. The second component of a hashing algorithm is collision resolution.

Ans. A hash table is a data structure that stores data associatively. Data is kept in an array format in a hash table, with each data item having its own unique index value.

Crack NTA UGC with Unacademy

Get subscription and access unlimited live and recorded courses from India’s best educators


  • Structured syllabus
  • Daily live classes
  • Ask doubts
  • Tests & practice
Learn more

Notifications

Get all the important information related to the NTA UGC Examination including the process of application, important calendar dates, eligibility criteria, exam centers etc.

Application Process
NTA UGC Results
UGC NET Admit Card
UGC NET Eligibility Criteria 2023
UGC NET Exam Pattern 2023: Paper 1 & Paper 2 Marking Scheme
See all

Related articles

Learn more topics related to Computer Science
What is the Instruction Cycle in Computer Architecture

The instruction cycle helps the CPU perform its primary job of expecting tasks. This article will discuss the steps involved in the instruction cycle and an example of the instruction cycle in detail.

What is Data Structure?

Data structure is putting together data in an organised manner. Data is arranged as primitive data structure, non-primitive data structure, and linear data structure.

Types of Registers

Different types of registers are available nowadays. Read this article to learn about Accumulator, MAR, MDR, and more.

Types of Queues and their Applications

Queue is a linear structure in which actions are conducted in a certain order. This article contains the study material notes on the types of queues - linear queue, circular queue, priority queue, dequeue, and their applications.

See all
Access more than

7,940+ courses for NTA-UGC-NET and SET Exams

Get subscription
freeliveclasses_ntaugc
testseries_ntaugcnet
Subscribe Now
.
Company Logo

Unacademy is India’s largest online learning platform. Download our apps to start learning


Starting your preparation?

Call us and we will answer all your questions about learning on Unacademy

Call +91 8585858585

Company
About usShikshodayaCareers
we're hiring
BlogsPrivacy PolicyTerms and Conditions
Help & support
User GuidelinesSite MapRefund PolicyTakedown PolicyGrievance Redressal
Products
Learner appLearner appEducator appEducator appParent appParent app
Popular goals
IIT JEEUPSCSSCCSIR UGC NETNEET UG
Trending exams
GATECATCANTA UGC NETBank Exams
Study material
UPSC Study MaterialNEET UG Study MaterialCA Foundation Study MaterialJEE Study MaterialSSC Study Material

© 2025 Sorting Hat Technologies Pvt Ltd

Unacademy
  • Goals
    • AFCAT
    • AP EAMCET
    • Bank Exam
    • BPSC
    • CA Foundation
    • CAPF
    • CAT
    • CBSE Class 11
    • CBSE Class 12
    • CDS
    • CLAT
    • CSIR UGC
    • GATE
    • IIT JAM
    • JEE
    • Karnataka CET
    • Karnataka PSC
    • Kerala PSC
    • MHT CET
    • MPPSC
    • NDA
    • NEET PG
    • NEET UG
    • NTA UGC
    • Railway Exam
    • SSC
    • TS EAMCET
    • UPSC
    • WBPSC
    • CFA

Share via

COPY