Hash Table Visualizer
Chaining | Simple Hash | Load Factor: 45.5%
5/11 entries
Hash Table Structure (Chaining)
0
empty
1
city: NYC
h(city) = 1
2
empty
3
empty
4
age: 25
hobby: Reading
h(age) = 4
5
empty
6
empty
7
job: Engineer
h(job) = 7
8
empty
9
empty
10
name: Alice
h(name) = 10
Normal EntryCollisionHighlighted
Logs
No operations yet
Notes
Hash table stores key-value pairs with O(1) average access time.
Chaining: Each bucket can hold multiple entries in a list.
Open Addressing: Collisions resolved by probing other slots.
Hash Functions: Simple (sum of char codes) vs DJB2 (better distribution).
Orange entries indicate collision resolution.
Load factor affects performance - rehashing needed when too high.
Toggle between methods to see different collision handling.
Implementation (TypeScript)
class HashTable<T> {
private buckets: Array<Array<{key: string, value: T}>> = [];
private capacity: number;
constructor(capacity = 11) {
this.capacity = capacity;
this.buckets = Array.from({length: capacity}, () => []);
}
private hash(key: string): number {
let hash = 0;
for (let i = 0; i < key.length; i++) {
hash += key.charCodeAt(i);
}
return hash % this.capacity;
}
set(key: string, value: T): void {
const index = this.hash(key);
const bucket = this.buckets[index];
const existing = bucket.find(entry => entry.key === key);
if (existing) {
existing.value = value; // Update
} else {
bucket.push({ key, value }); // Insert
}
}
get(key: string): T | undefined {
const index = this.hash(key);
const bucket = this.buckets[index];
const entry = bucket.find(entry => entry.key === key);
return entry?.value;
}
delete(key: string): boolean {
const index = this.hash(key);
const bucket = this.buckets[index];
const entryIndex = bucket.findIndex(entry => entry.key === key);
if (entryIndex !== -1) {
bucket.splice(entryIndex, 1);
return true;
}
return false;
}
}About Hash Tables
A hash table uses a hash function to map keys to array indices, enabling fast key-value operations.
Collision Resolution:
- Chaining: Store colliding items in linked lists
- Open Addressing: Probe for next available slot
Applications:
- Database indexing
- Caching systems
- Set implementations
- Symbol tables in compilers
Time Complexity: O(1) average, O(n) worst case
Space Complexity: O(n)