Found 8862 Articles for Front End Technology

The HashTable Class in Javascript

Sai Subramanyam
Updated on 15-Jun-2020 10:58:43

96 Views

Here is the complete implementation of the HashTable class. This could of course be improved by using more efficient data structures and collision resolution algorithms.Exampleclass HashTable {    constructor() {       this.container = [];       // Populate the container with empty arrays       // which can be used to add more elements in       // cases of collisions       for (let i = 0; i < 11; i++) {          this.container.push([]);       }    }    display() {       this.container.forEach((value, index) => ... Read More

Joining two hash tables in Javascript

karthikeya Boyini
Updated on 15-Jun-2020 11:00:35

398 Views

Sometimes we need to combine containers together using a join function and get a new container. We'll write a static join method that takes in 2 HashTables and creates a new HashTable with all the values. For the sake of simplicity, we'll let the values from the second one override the values for the first one if there are any keys present in both of them. Examplestatic join(table1, table2) {    // Check if both args are HashTables    if(!table1 instanceof HashTable || !table2 instanceof HashTable) {       throw new Error("Illegal Arguments")    }    let combo = ... Read More

Loop through a hash table using Javascript

Sai Subramanyam
Updated on 15-Jun-2020 11:01:51

1K+ Views

Now let us create a forEach function that'll allow us to loop over all key-value pairs and call a callback on those values. For this, we just need to loop over each chain in the container and call the callback on the key and value pairs.ExampleforEach(callback) {    // For each chain    this.container.forEach(elem => {       // For each element in each chain call callback on KV pair       elem.forEach(({ key, value }) => callback(key, value));    }); }You can test this using.Examplelet ht = new HashTable(); ht.put(10, 94); ht.put(20, 72); ht.put(30, 1); ht.put(21, 6); ... Read More

Remove elements from Javascript Hash Table

karthikeya Boyini
Updated on 15-Jun-2020 11:04:30

954 Views

To remove elements, we simply need to find them and remove them using a simple splice function call that removes elements in place from an array.Let us look at the implementation of the same − Exampleremove(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; }You can test this using − Examplelet ht = ... Read More

Search Element in an Javascript Hash Table

Sai Subramanyam
Updated on 15-Jun-2020 11:06:23

260 Views

We've kind of already implemented this in our put method. Let us look at it again in isolation.Exampleget(key) {    let hashCode = 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) {          return this.container[hashCode][i];       }    }    return undefined; }You can test it using.Examplelet 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.get(21)); console.log(ht.get(55)); console.log(ht.get(32));OutputThis will give the output.{ key: 20, ... Read More

Add elements to a hash table using Javascript

karthikeya Boyini
Updated on 15-Jun-2020 11:08:45

769 Views

When adding elements to a hash table the most crucial part is collision resolution. We're going to use chaining for the same. There are other algorithms you can read about here: https://en.wikipedia.org/wiki/Hash_table#Collision_resolutionNow let's look at the implementation. We'll be creating a hash function that'll work on integers only to keep this simple. But a more complex algorithm can be used to hash every object − Exampleput(key, value) {    let hashCode = hash(key);    for(let i = 0; i < this.container[hashCode].length; i ++) {       // Replace the existing value with the given key       ... Read More

Creating a hash table using Javascript

karthikeya Boyini
Updated on 15-Jun-2020 11:09:18

304 Views

Let us set up a simple class that we'll use to define all these methods on. We'll create a container object to store the hash table and create a display function to display the table. Note that for collision resolution, we'll use chaining.The display function takes each entry (hashed value) in the table and prints all pairs associated with it.ExampleWe'll also create a new class on the prototype to store the key-value pairs.class HashTable {    constructor() {       this.container = [];       // Populate the container with empty arrays       // which can ... Read More

Hash Table Data Structure in Javascript

Samual Sam
Updated on 15-Jun-2020 10:31:03

1K+ Views

Hash Table is a data structure which stores data in an associative manner. In a hash table, data is stored in an array format, where each data value has its own unique index value. Access to data becomes very fast if we know the index of the desired data.Thus, it becomes a data structure in which insertion and search operations are very fast irrespective of the size of the data. Hash Table uses an array as a storage medium and uses the hash technique to generate an index where an element is to be inserted or is to be located ... Read More

The Dictionary Class in Javascript

Samual Sam
Updated on 15-Jun-2020 10:31:45

357 Views

Here is the complete implementation of MyMap class − Exampleclass MyMap {    constructor() {       this.container = {};    }    display() {       console.log(this.container);    }    hasKey(key) {       return key in this.container;    }    put(key, value) {       this.container[key] = value;    }    delete(key) {       if (this.hasKey(key)) {          delete this.container[key];          return true;       }       return false;    }    get(key) {       return this.hasKey(key) ? this.container[key] : undefined;   ... Read More

Loop through a Dictionary in Javascript

Monica Mona
Updated on 15-Jun-2020 10:34:46

10K+ Views

Here we'll implement a for each function in our class and accept a callback that we can call on every key-value pair. Let's see how we can implement such a function − ExampleforEach(callback) {    for (let prop in this.container) {       // Call the callback as: callback(key, value)       callback(prop, this.container[prop]);    } }You can test this using − Exampleconst myMap = new MyMap(); myMap.put("key1", "value1"); myMap.put("key2", "value2"); myMap.forEach((k, v) => console.log(`Key is ${k} and value is ${v}`));OutputThis will give the output −Key is key1 and value is value1 Key is key2 and value is value2ES6 ... Read More

Advertisements