Remove elements from Javascript Hash Table

To remove elements from a JavaScript hash table, we need to locate the element using its key and remove it from the underlying storage structure. In hash tables that use chaining for collision resolution, this involves searching through the chain at the computed hash index.

Let us look at the implementation of the remove method:

Remove Method Implementation

remove(key) {
    let hashCode = this.hash(key);

    for (let i = 0; i < this.container[hashCode].length; i++) {
        // Find the element in the chain
        if (this.container[hashCode][i].key === key) {
            this.container[hashCode].splice(i, 1);
            return true;
        }
    }
    return false;
}

How It Works

The remove method works by:

  • Computing the hash code for the given key
  • Iterating through the chain at that hash index
  • Finding the element with the matching key
  • Using splice() to remove the element in place
  • Returning true if found and removed, false otherwise

Complete Example

Here's how to test the remove functionality:

class HashTable {
    constructor() {
        this.container = {};
    }
    
    hash(key) {
        return key % 10; // Simple hash function
    }
    
    put(key, value) {
        let hashCode = this.hash(key);
        if (this.container[hashCode] == undefined) {
            this.container[hashCode] = [];
        }
        this.container[hashCode].push({key: key, value: value});
    }
    
    get(key) {
        let hashCode = this.hash(key);
        if (this.container[hashCode] != undefined) {
            for (let i = 0; i < this.container[hashCode].length; i++) {
                if (this.container[hashCode][i].key === key) {
                    return this.container[hashCode][i];
                }
            }
        }
        return undefined;
    }
    
    remove(key) {
        let hashCode = this.hash(key);
        
        if (this.container[hashCode] == undefined) {
            return false;
        }

        for (let i = 0; i < this.container[hashCode].length; i++) {
            if (this.container[hashCode][i].key === key) {
                this.container[hashCode].splice(i, 1);
                return true;
            }
        }
        return false;
    }
}

let ht = new HashTable();

ht.put(10, 94);
ht.put(20, 72);
ht.put(30, 1);
ht.put(21, 6);
ht.put(15, 21);
ht.put(32, 34);

console.log(ht.get(20));
console.log(ht.remove(20));
console.log(ht.get(20));
console.log(ht.remove(20));
{ key: 20, value: 72 }
true
undefined
false

Key Points

  • The first call to remove(20) returns true because the element was found and successfully deleted
  • The second call to remove(20) returns false because the element no longer exists
  • The splice() method modifies the array in place, removing the element at the specified index
  • Always check if the hash bucket exists before attempting to iterate through it

Conclusion

Removing elements from a hash table involves locating the key in its hash bucket and using splice() to remove it from the chain. The method returns a boolean indicating success or failure of the removal operation.

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

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements