Algorithm
Code Examples
#1 Simple example of a hash table implemented in Python:
Code -
Python Programming
class HashTable:
def __init__(self, size):
self.size = size
self.table = [None] * size
def _hash_function(self, key):
# A simple hash function for demonstration purposes
return hash(key) % self.size
def insert(self, key, value):
index = self._hash_function(key)
if self.table[index] is None:
self.table[index] = [(key, value)]
else:
# Handling collisions using chaining (each bucket is a list of key-value pairs)
for pair in self.table[index]:
if pair[0] == key:
# Update the value if the key already exists
pair = (key, value)
break
else:
# If key doesn't exist in the bucket, append it
self.table[index].append((key, value))
def get(self, key):
index = self._hash_function(key)
if self.table[index] is not None:
for pair in self.table[index]:
if pair[0] == key:
return pair[1] # Return the value associated with the key
raise KeyError(f"Key not found: {key}")
def remove(self, key):
index = self._hash_function(key)
if self.table[index] is not None:
for i, pair in enumerate(self.table[index]):
if pair[0] == key:
del self.table[index][i] # Remove the key-value pair
return
raise KeyError(f"Key not found: {key}")
# Example usage:
hash_table = HashTable(size=10)
hash_table.insert("name", "John")
hash_table.insert("age", 25)
hash_table.insert("city", "New York")
print("Value for 'name':", hash_table.get("name"))
print("Value for 'age':", hash_table.get("age"))
print("Value for 'city':", hash_table.get("city"))
hash_table.remove("age")
# This will raise a KeyError because "age" has been removed
# print("Value for 'age':", hash_table.get("age"))
Copy The Code &
Try With Live Editor
#2 Simple example of using a hash table in Java
Code -
Java Programming
import java.util.HashMap;
import java.util.Map;
public class HashTableExample {
public static void main(String[] args) {
// Create a HashMap
Map < String, Integer> hashMap = new HashMap<>();
// Adding key-value pairs to the hash table
hashMap.put("John", 25);
hashMap.put("Jane", 30);
hashMap.put("Doe", 22);
// Accessing values using keys
System.out.println("John's age: " + hashMap.get("John"));
System.out.println("Jane's age: " + hashMap.get("Jane"));
// Updating the value for a key
hashMap.put("John", 26);
System.out.println("Updated John's age: " + hashMap.get("John"));
// Removing a key-value pair
hashMap.remove("Doe");
// Checking if a key is present
System.out.println("Is Jane present? " + hashMap.containsKey("Jane"));
// Iterating through the hash table
System.out.println("Iterating through the hash table:");
for (Map.Entry < String, Integer> entry : hashMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
Copy The Code &
Try With Live Editor
#3 Simple example of using a hash table in C:
Code -
C Programming
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TABLE_SIZE 10
// Define a structure for a key-value pair
typedef struct {
char key[50];
int value;
} KeyValuePair;
// Define a structure for the hash table
typedef struct {
KeyValuePair* table[TABLE_SIZE];
} HashTable;
// Hash function to convert a key into an index
unsigned int hash(char* key) {
unsigned int hash_value = 0;
for (int i = 0; i < strlen(key); i++) {
hash_value = hash_value * 31 + key[i];
}
return hash_value % TABLE_SIZE;
}
// Function to initialize a hash table
void initializeHashTable(HashTable* ht) {
for (int i = 0; i < TABLE_SIZE; i++) {
ht->table[i] = NULL;
}
}
// Function to insert a key-value pair into the hash table
void insertIntoHashTable(HashTable* ht, char* key, int value) {
unsigned int index = hash(key);
// Create a new key-value pair
KeyValuePair* new_pair = (KeyValuePair*)malloc(sizeof(KeyValuePair));
strcpy(new_pair->key, key);
new_pair->value = value;
// Insert the new pair into the hash table
ht->table[index] = new_pair;
}
// Function to retrieve the value associated with a key from the hash table
int getFromHashTable(HashTable* ht, char* key) {
unsigned int index = hash(key);
// Retrieve the key-value pair from the hash table
KeyValuePair* pair = ht->table[index];
// Check if the key exists in the hash table
if (pair != NULL && strcmp(pair->key, key) == 0) {
return pair->value;
} else {
// Key not found
return -1;
}
}
int main() {
// Create a hash table
HashTable myHashTable;
initializeHashTable(&myHashTable);
// Insert key-value pairs into the hash table
insertIntoHashTable(&myHashTable, "one", 1);
insertIntoHashTable(&myHashTable, "two", 2);
insertIntoHashTable(&myHashTable, "three", 3);
// Retrieve values from the hash table
printf("Value for key 'one': %d\n", getFromHashTable(&myHashTable, "one"));
printf("Value for key 'two': %d\n", getFromHashTable(&myHashTable, "two"));
printf("Value for key 'three': %d\n", getFromHashTable(&myHashTable, "three"));
printf("Value for key 'four': %d\n", getFromHashTable(&myHashTable, "four")); // Key not found
return 0;
}
Copy The Code &
Try With Live Editor
#4 Simple example of using a hash table in C++
Code -
C++ Programming
#include <iostream>
#include <unordered_map>
int main() {
// Creating a hash table (unordered_map) with string keys and int values
std::unordered_map < std::string, int> hashTable;
// Inserting values into the hash table
hashTable["apple"] = 5;
hashTable["banana"] = 3;
hashTable["orange"] = 7;
// Accessing values using keys
std::cout << "Number of apples: " << hashTable["apple"] << std::endl;
std::cout << "Number of bananas: " << hashTable["banana"] << std::endl;
// Checking if a key exists in the hash table
std::string keyToCheck = "grape";
if (hashTable.find(keyToCheck) != hashTable.end()) {
std::cout << "Number of " << keyToCheck << "s: " << hashTable[keyToCheck] << std::endl;
} else {
std::cout << keyToCheck << " not found in the hash table." << std::endl;
}
// Iterating through the hash table
std::cout << "Iterating through the hash table:" << std::endl;
for (const auto& entry : hashTable) {
std::cout << entry.first << ": " << entry.second << std::endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Demonstration
Hash Table - Data Structure and Algorithms-DevsEnv