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 

Similar Reads

Why use Hashing?

In hashing, all the operations like inserting, searching, and deleting can be performed in O(1) i.e. constant time. The worst case time complexity for hashing remains O(n) but the average case time complexity is O(1)....

HashTable

Used in order to create a new hash table....

Basic Operations with Syntax

Get:...

Components of Hashing in Javascript

1. Hash Table: A hash table is a generalization of the array. It gives the functionality in which a collection of data is stored in such a way that it is easy to find those items later if required. This makes searching for an element very efficient....

Contact Us