Photo AI
Last Updated Sep 27, 2025
Revision notes with simplified explanations to understand Hash Tables quickly and effectively.
217+ students studying
A hash table is a data structure that stores key-value pairs. It provides fast access to data using a process called hashing, which maps keys to indices in an underlying array. Hash tables are widely used for implementing associative arrays, databases, and caches due to their average time complexity of O(1) for insertion, deletion, and search operations.
Example: Hash table with separate chaining.
# Simple hash table using arrays and linked lists for separate chaining
SIZE = 10
hash_table = [[] for _ in range(SIZE)]
def hash_function(key):
return key % SIZE
def insert(hash_table, key, value):
index = hash_function(key)
for pair in hash_table[index]:
if pair[0] == key:
pair[1] = value # Update value if key exists
return
hash_table[index].append([key, value]) # Insert new key-value pair
def search(hash_table, key):
index = hash_function(key)
for pair in hash_table[index]:
if pair[0] == key:
return pair[1] # Return value if key is found
return None # Key not found
def delete(hash_table, key):
index = hash_function(key)
for i, pair in enumerate(hash_table[index]):
if pair[0] == key:
del hash_table[index][i] # Remove key-value pair
return
Example: A hash table class using separate chaining.
class HashTable:
def __init__(self, size=10):
self.size = size
self.table = [[] for _ in range(size)]
def hash_function(self, key):
return hash(key) % self.size
def insert(self, key, value):
index = self.hash_function(key)
for pair in self.table[index]:
if pair[0] == key:
pair[1] = value # Update value if key exists
return
self.table[index].append([key, value]) # Insert new key-value pair
def search(self, key):
index = self.hash_function(key)
for pair in self.table[index]:
if pair[0] == key:
return pair[1]
return None
def delete(self, key):
index = self.hash_function(key)
for i, pair in enumerate(self.table[index]):
if pair[0] == key:
del self.table[index][i]
return
While hash tables are not typically used for sequential access, traversal can be done by iterating through all buckets.
Example:
def traverse(hash_table):
for i, bucket in enumerate(hash_table):
if bucket:
print(f"Bucket {i}: {bucket}")
Inserting and Searching in a Hash Table:
hash_table = HashTable()
hash_table.insert("name", "Alice")
hash_table.insert("age", 25)
print(hash_table.search("name")) # Output: Alice
print(hash_table.search("age")) # Output: 25
Deleting an Entry:
hash_table.delete("name")
print(hash_table.search("name")) # Output: None
Traversing the Hash Table:
traverse(hash_table.table)
Enhance your understanding with flashcards, quizzes, and exams—designed to help you grasp key concepts, reinforce learning, and master any topic with confidence!
90 flashcards
Flashcards on Hash Tables
Revise key concepts with interactive flashcards.
Try Computer Science Flashcards9 quizzes
Quizzes on Hash Tables
Test your knowledge with fun and engaging quizzes.
Try Computer Science Quizzes29 questions
Exam questions on Hash Tables
Boost your confidence with real exam questions.
Try Computer Science Questions27 exams created
Exam Builder on Hash Tables
Create custom exams across topics for better practice!
Try Computer Science exam builder12 papers
Past Papers on Hash Tables
Practice past papers to reinforce exam experience.
Try Computer Science Past PapersDiscover More Revision Notes Related to Hash Tables to Deepen Your Understanding and Improve Your Mastery
Join 500,000+ A-Level students using SimpleStudy...
Join Thousands of A-Level Students Using SimpleStudy to Learn Smarter, Stay Organized, and Boost Their Grades with Confidence!
Report Improved Results
Recommend to friends
Students Supported
Questions answered