Basic Operations with Syntax
Get:
Used to search a key inside the hash table and return the value that is associated with that key.
Syntax:
// function inside hashtable class to
// access a value using its key
get(key) {
const target = this._setKey(key);
return this.table[target];
}
Insert:
Used to insert a new key-value pair inside the hash table.
Syntax:
// function to insert a value in the
// hash table using setKey function
insert(value) {
const index = this._setKey(value);
this.table[index] = value;
this.size++;
}
Search:
Used to search for a value.
Syntax:
// search a value (by getting its
// key using setKey function)
search(value){
const index = this._setKey(value);
if(this.table[index]==value)
console.log("The value is found at index : ",index);
else
console.log("Not found");
}
Delete:
Used in order to delete a particular key-value pair from the hash table.
Syntax:
// function to delete a key from the hashtable
delete (key) {
const index = this._setKey(key);
if (this.table[index]) {
this.table[index] = [];
this.size--;
return true;
} else {
return false;
}
}
Hashing in JavaScript
Hashing is a popular technique used for storing and retrieving data as fast as possible. The main reason behind using hashing is that it performs insertion, deletion, searching, and other operations
Contact Us