Hash Table Insert
Data Structuresmedium#15Map keys to buckets via hash function — O(1) average lookup
Ready
Step 1 of 0
Speed
Input Data
Comma-separated numbers (0-999). Min 2 values.
Algorithm Code
1class HashTable {2 constructor(size) {3 this.buckets = Array.from({length: size}, () => []);4 this.size = size;5 }6 hash(key) { return key % this.size; }7 set(key, value) {8 const idx = this.hash(key);9 const existing = this.buckets[idx].find(e => e.key === key);10 if (existing) existing.value = value;11 else this.buckets[idx].push({ key, value });12 }13 get(key) {14 const idx = this.hash(key);15 return this.buckets[idx].find(e => e.key === key)?.value;16 }17}
Best
O(1)
Average
O(1)
Worst
O(n)
Space
O(n)
About Hash Table Insert
A Hash Table maps keys to values using a hash function that computes an index into an array of buckets. Collisions (multiple keys mapping to the same bucket) are resolved via chaining (linked lists) or open addressing. Average O(1) lookup makes it the most-used data structure in software engineering.
Time Complexity: Best: O(1), Average: O(1), Worst: O(n). Space: O(n).