Open In App

Hash Table Data Structure

What is Hash Table?

A Hash table is defined as a data structure used to insert, look up, and remove key-value pairs quickly. It operates on the hashing concept, where each key is translated by a hash function into a distinct index in an array. The index functions as a storage location for the matching value. In simple words, it maps the keys with the value.

What is Load factor?

A hash table’s load factor is determined by how many elements are kept there in relation to how big the table is. The table may be cluttered and have longer search times and collisions if the load factor is high. An ideal load factor can be maintained with the use of a good hash function and proper table resizing.



What is a Hash function?

A Function that translates keys to array indices is known as a hash function. The keys should be evenly distributed across the array via a decent hash function to reduce collisions and ensure quick lookup speeds.

Choosing a hash function:

Selecting a decent hash function is based on the properties of the keys and the intended functionality of the hash table. Using a function that evenly distributes the keys and reduces collisions is crucial.



Criteria based on which a hash function is chosen:

Collision resolution techniques:

Collisions happen when two or more keys point to the same array index. Chaining, open addressing, and double hashing are a few techniques for resolving collisions.

Dynamic resizing:

This feature enables the hash table to expand or contract in response to changes in the number of elements contained in the table. This promotes a load factor that is ideal and quick lookup times.

Implementations of Hash Table

Python, Java, C++, and Ruby are just a few of the programming languages that support hash tables. They can be used as a customised data structure in addition to frequently being included in the standard library.

Example- Count characters in the String “geeksforgeeks”.

In this example, we use a hashing technique for storing the count of the string.




#include <bits/stdc++.h>
using namespace std;
 
int main() {
  //initialize a string
  string s="geeksforgeeks";
   
  // Using an array to store the count of each alphabet
  // by mapping the character to an index value
 
  int arr[26]={0};
   
  //Storing the count
  for(int i=0;i<s.size();i++){
    arr[s[i]-'a']++;
  }
   
  //Search the count of the character
  char ch='e';
   
  // get count
  cout<<"The count of ch is "<<arr[ch-'a']<<endl;
  return 0;
}




public class CharacterCount {
    public static void main(String[] args) {
        // Initialize a string
        String s = "geeksforgeeks";
 
        // Using an array to store the count of each alphabet
        // by mapping the character to an index value
 
        int[] arr = new int[26];
 
        // Storing the count
        for (int i = 0; i < s.length(); i++) {
            arr[s.charAt(i) - 'a']++;
        }
 
        // Search the count of the character
        char ch = 'e';
 
        // Get count
        System.out.println("The count of " + ch + " is " + arr[ch - 'a']);
    }
}




# Initialize a string
s = "geeksforgeeks"
 
# Using a list to store the count of each alphabet
# by mapping the character to an index value
arr = [0] * 26
 
# Storing the count
for i in range(len(s)):
    arr[ord(s[i]) - ord('a')] += 1
 
# Search the count of the character
ch = 'e'
 
# Get count
print("The count of", ch, "is", arr[ord(ch) - ord('a')])




using System;
 
class Program {
    static void Main(string[] args) {
        //initialize a string
        string s = "geeksforgeeks";
        // Using an array to store the count of each alphabet
        // by mapping the character to an index value
        int[] arr = new int[26];
        //Storing the count
        for (int i = 0; i < s.Length; i++) {
            arr[s[i] - 'a']++;
        }
        //Search the count of the character
        char ch = 'e';
        // get count
        Console.WriteLine("The count of ch is " + arr[ch - 'a']);
    }
}




// Initialize a string
const s = "geeksforgeeks";
 
// Using an array to store the count of each alphabet
// by mapping the character to an index value
 
const arr = Array(26).fill(0);
 
// Storing the count
for (let i = 0; i < s.length; i++) {
  arr[s.charCodeAt(i) - 'a'.charCodeAt(0)]++;
}
 
// Search the count of the character
const ch = 'e';
 
// Get count
console.log(`The count of ${ch} is ${arr[ch.charCodeAt(0) - 'a'.charCodeAt(0)]}`);

Output
The count of ch is 4





Complexity Analysis of a Hash Table:

For lookup, insertion, and deletion operations, hash tables have an average-case time complexity of O(1). Yet, these operations may, in the worst case, require O(n) time, where n is the number of elements in the table.

Applications of Hash Table:


Article Tags :