Skip to content

Instantly share code, notes, and snippets.

@keif
Last active February 21, 2025 18:47
Show Gist options
  • Save keif/92f73a951b67b715f5917d1fb49d4d7b to your computer and use it in GitHub Desktop.
Save keif/92f73a951b67b715f5917d1fb49d4d7b to your computer and use it in GitHub Desktop.
In Javascript, create a hash table from scratch without using Map or Set.
function createHashTable(size = 53) {
// Initialize an array of given size to store key-value pairs (buckets)
const buckets = new Array(size);
// Hash function to convert a key into an index
function hash(key) {
let hash = 0;
for (let i = 0; i < key.length; i++) {
hash = (hash + key.charCodeAt(i) * 23) % size;
}
return hash;
}
// Insert or update a key-value pair in the hash table
function set(key, value) {
const index = hash(key);
// If no bucket exists at this index, create an empty array
if (!buckets[index]) {
buckets[index] = [];
}
// Check if key already exists and update its value
for (let pair of 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
buckets[index].push([key, value]);
}
// Retrieve a value by its key
function get(key) {
const index = hash(key);
// If no bucket exists at this index, return undefined
if (!buckets[index]) return undefined;
// Search for the key in the bucket and return its value if found
for (let pair of 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
function remove(key) {
const index = hash(key);
// If no bucket exists at this index, return false (key not found)
if (!buckets[index]) return false;
// Iterate through the bucket and remove the key-value pair if found
for (let i = 0; i < buckets[index].length; i++) {
if (buckets[index][i][0] === key) {
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
function keys() {
const keysArray = [];
// Iterate through all buckets and collect keys
for (let bucket of buckets) {
if (bucket) {
for (let pair of bucket) {
keysArray.push(pair[0]);
}
}
}
return keysArray;
}
// Return an array of all values in the hash table
function values() {
const valuesArray = [];
// Iterate through all buckets and collect values
for (let bucket of buckets) {
if (bucket) {
for (let pair of bucket) {
valuesArray.push(pair[1]);
}
}
}
return valuesArray;
}
// Return an object exposing hash table operations
return { set, get, remove, keys, values };
}
// Example usage:
const ht = createHashTable();
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.remove("age")); // true
console.log(ht.get("age")); // undefined
console.log(ht.keys()); // ["name", "occupation"]
console.log(ht.values()); // ["Alice", "Engineer"]
@keif
Copy link
Author

keif commented Feb 21, 2025

This approach avoids using class and leverages function closures to encapsulate the hash table behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment