1const M = 7; // table length
2const h1 = (key) => key % M;
3// Method 1: open addressing (linear probing)
4function insertLinear(table, key) {
5 let i = h1(key);
6 while (table[i] !== null) i = (i + 1) % M;
7 table[i] = key;
8 return i;
9}
10// Method 2: chaining
11function insertChain(table, key) {
12 table[h1(key)].push(key);
13}
14// Method 3: rehashing (double hashing)
15const h2 = (key) => 5 - (key % 5);
16function insertDouble(table, key) {
17 let i = h1(key), step = h2(key), k = 0;
18 while (table[i] !== null) { k++; i = (h1(key) + k * step) % M; }
19 table[i] = key;
20 return i;
21}