Joining two hash tables in Javascript

Sometimes we need to combine hash tables together using a join function to create a new merged hash table. We'll write a static join method that takes 2 HashTables and creates a new HashTable with all the values. For simplicity, values from the second hash table will override values from the first one if there are duplicate keys.

Syntax

static join(table1, table2) {
    // Implementation logic
    return newHashTable;
}

Implementation

Here's the complete implementation of the join method:

class HashTable {
    constructor() {
        this.table = new Array(11);
        this.size = 0;
    }

    hash(key) {
        return key % this.table.length;
    }

    put(key, value) {
        const index = this.hash(key);
        if (!this.table[index]) {
            this.table[index] = {};
        }
        this.table[index][key] = value;
        this.size++;
    }

    forEach(callback) {
        for (let i = 0; i < this.table.length; i++) {
            if (this.table[i]) {
                for (let key in this.table[i]) {
                    callback(key, this.table[i][key]);
                }
            }
        }
    }

    display() {
        for (let i = 0; i < this.table.length; i++) {
            let bucket = this.table[i];
            if (bucket) {
                let bucketStr = Object.keys(bucket).map(key => `{ ${key}: ${bucket[key]} }`).join(' --> ');
                console.log(`${i}: ${bucketStr}`);
            } else {
                console.log(`${i}:`);
            }
        }
    }

    static join(table1, table2) {
        // Check if both args are HashTables
        if (!(table1 instanceof HashTable) || !(table2 instanceof HashTable)) {
            throw new Error("Illegal Arguments");
        }

        let combo = new HashTable();
        table1.forEach((k, v) => combo.put(k, v));
        table2.forEach((k, v) => combo.put(k, v));
        return combo;
    }
}

Example Usage

Let's test the join method with two hash tables:

let ht1 = new HashTable();
ht1.put(10, 94);
ht1.put(20, 72);
ht1.put(30, 1);

let ht2 = new HashTable();
ht2.put(21, 6);
ht2.put(15, 21);
ht2.put(32, 34);

let htCombo = HashTable.join(ht1, ht2);
htCombo.display();
0:
1:
2:
3:
4: { 15: 21 }
5:
6:
7:
8: { 30: 1 }
9: { 20: 72 }
10: { 10: 94 } --> { 21: 6 } --> { 32: 34 }

Key Points

  • The join method validates both parameters are HashTable instances
  • Creates a new HashTable instead of modifying existing ones
  • Values from the second table override duplicate keys from the first table
  • Uses forEach to iterate through all key-value pairs efficiently

Conclusion

The static join method provides a clean way to merge two hash tables while handling key collisions by letting the second table's values take precedence. This approach maintains data integrity and creates a new hash table without modifying the original ones.

Updated on: 2026-03-15T23:18:59+05:30

676 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements