Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Add elements to a hash table using Javascript
When adding elements to a hash table, the most crucial part is collision resolution. We're going to use chaining for the same. There are other algorithms you can read about here: https://en.wikipedia.org/wiki/Hash_table#Collision_resolution
Now let's look at the implementation. We'll be creating a hash function that'll work on integers only to keep this simple. But a more complex algorithm can be used to hash every object.
Hash Table Implementation
First, let's create a complete hash table class with the necessary components:
class HashTable {
constructor(size = 11) {
this.size = size;
this.container = new Array(size);
// Initialize each bucket as an empty array
for (let i = 0; i < size; i++) {
this.container[i] = [];
}
}
// Key-Value pair constructor
KVPair = class {
constructor(key, value) {
this.key = key;
this.value = value;
}
}
// Simple hash function for integers
hash(key) {
return key % this.size;
}
// Add elements to hash table
put(key, value) {
let hashCode = this.hash(key);
for(let i = 0; i < this.container[hashCode].length; i++) {
// Replace the existing value with the given key
// if it already exists
if(this.container[hashCode][i].key === key) {
this.container[hashCode][i].value = value;
return;
}
}
// Push the pair at the end of the array
this.container[hashCode].push(new this.KVPair(key, value));
}
// Display the hash table
display() {
for (let i = 0; i < this.size; i++) {
let bucket = this.container[i];
let output = i + ":";
for (let j = 0; j < bucket.length; j++) {
output += ` { ${bucket[j].key}: ${bucket[j].value} }`;
if (j < bucket.length - 1) {
output += " -->";
}
}
console.log(output);
}
}
}
Example Usage
You can test this implementation using the following code:
let ht = new HashTable(); ht.put(10, 94); ht.put(20, 72); ht.put(30, 1); ht.put(21, 6); ht.put(15, 21); ht.put(32, 34); ht.display();
0:
1:
2:
3:
4: { 15: 21 }
5:
6:
7:
8: { 30: 1 }
9: { 20: 72 }
10: { 10: 94 } -->{ 21: 6 } -->{ 32: 34 }
How It Works
The put() method works in two steps:
- Check for existing key: It first searches the bucket to see if the key already exists. If found, it updates the value.
- Add new entry: If the key doesn't exist, it adds a new key-value pair to the bucket.
Notice how keys 10, 21, and 32 all hash to bucket 10 (10 % 11 = 10, 21 % 11 = 10, 32 % 11 = 10), demonstrating collision resolution through chaining.
Key Points
- Uses chaining to handle collisions - multiple elements can exist in the same bucket
- Updates existing values when the same key is inserted again
- Simple modulo hash function for integer keys
- Each bucket is an array that can store multiple key-value pairs
Conclusion
The put() method efficiently adds elements to a hash table using chaining for collision resolution. It handles both new insertions and updates to existing keys, ensuring data integrity.
