Hash Table Open Addressing Double Hash (JavaScript)

Implement a hash table with open addressing and double hashing for collision resolution supporting insert, lookup, and delete.

JavascriptData Integrity

Code

snippet
class HashTable {
  constructor(size = 101) {
    this.size = size;
    this.keys = new Array(size).fill(null);
    this.vals = new Array(size).fill(null);
  }
  h1(key) { let h = 0; for (const c of String(key)) h = (h * 31 + c.charCodeAt(0)) % this.size; return h; }
  h2(key) { let h = 0; for (const c of String(key)) h = (h * 17 + c.charCodeAt(0)) % (this.size - 1); return h + 1; }
  findSlot(key) {
    const step = this.h2(key);
    let idx = this.h1(key);
    for (let i = 0; i < this.size; i++) {
      if (this.keys[idx] === null || this.keys[idx] === key) return idx;
      idx = (idx + step) % this.size;
    }
    throw new Error("table full");
  }
  set(key, val) { const i = this.findSlot(key); this.keys[i] = key; this.vals[i] = val; }
  get(key) { const i = this.findSlot(key); return this.keys[i] === key ? this.vals[i] : undefined; }
}