Last active
February 21, 2025 18:47
-
-
Save keif/1bf698c47dfe368b32349186f079721b to your computer and use it in GitHub Desktop.
In Javascript, create a hash table from scratch without using Map or Set.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HashTable { | |
constructor(size = 53) { | |
// Initialize an array of given size to store key-value pairs (buckets) | |
this.buckets = new Array(size); | |
this.size = size; | |
} | |
// Hash function to convert a key into an index | |
_hash(key) { | |
let hash = 0; | |
for (let i = 0; i < key.length; i++) { | |
hash = (hash + key.charCodeAt(i) * 23) % this.size; | |
} | |
return hash; | |
} | |
// Insert or update a key-value pair in the hash table | |
set(key, value) { | |
const index = this._hash(key); | |
// If no bucket exists at this index, create an empty array | |
if (!this.buckets[index]) { | |
this.buckets[index] = []; | |
} | |
// Check if key already exists and update its value | |
for (let pair of this.buckets[index]) { | |
if (pair[0] === key) { | |
pair[1] = value; // Update existing key with new value | |
return; | |
} | |
} | |
// If key does not exist, add a new key-value pair | |
this.buckets[index].push([key, value]); | |
} | |
// Retrieve a value by its key | |
get(key) { | |
const index = this._hash(key); | |
// If no bucket exists at this index, return undefined | |
if (!this.buckets[index]) return undefined; | |
// Search for the key in the bucket and return its value if found | |
for (let pair of this.buckets[index]) { | |
if (pair[0] === key) return pair[1]; | |
} | |
return undefined; // Return undefined if key is not found | |
} | |
// Delete a key-value pair from the hash table | |
delete(key) { | |
const index = this._hash(key); | |
// If no bucket exists at this index, return false (key not found) | |
if (!this.buckets[index]) return false; | |
// Iterate through the bucket and remove the key-value pair if found | |
for (let i = 0; i < this.buckets[index].length; i++) { | |
if (this.buckets[index][i][0] === key) { | |
this.buckets[index].splice(i, 1); | |
return true; // Key was successfully deleted | |
} | |
} | |
return false; // Return false if key was not found | |
} | |
// Return an array of all keys in the hash table | |
keys() { | |
const keysArray = []; | |
// Iterate through all buckets and collect keys | |
for (let bucket of this.buckets) { | |
if (bucket) { | |
for (let pair of bucket) { | |
keysArray.push(pair[0]); | |
} | |
} | |
} | |
return keysArray; | |
} | |
// Return an array of all values in the hash table | |
values() { | |
const valuesArray = []; | |
// Iterate through all buckets and collect values | |
for (let bucket of this.buckets) { | |
if (bucket) { | |
for (let pair of bucket) { | |
valuesArray.push(pair[1]); | |
} | |
} | |
} | |
return valuesArray; | |
} | |
} | |
// Example usage: | |
const ht = new HashTable(); | |
ht.set("name", "Alice"); | |
ht.set("age", 25); | |
ht.set("occupation", "Engineer"); | |
console.log(ht.get("name")); // Alice | |
console.log(ht.get("age")); // 25 | |
console.log(ht.delete("age")); // true | |
console.log(ht.get("age")); // undefined | |
console.log(ht.keys()); // ["name", "occupation"] | |
console.log(ht.values()); // ["Alice", "Engineer"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This implementation ensures basic hash table functionality with separate chaining for collision resolution.