Samual Sam has Published 2492 Articles

Dictionary Data Structure in Javascript

Samual Sam

Samual Sam

Updated on 15-Jun-2020 10:53:14

1K+ Views

In computer science, an associative array, map, symbol table, or dictionary is an abstract data type composed of a collection of (key, value) pairs, such that each possible key appears at most once in the collection. Note that a dictionary is also known as a map.The dictionary problem is a ... Read More

Remove elements from a Dictionary using Javascript

Samual Sam

Samual Sam

Updated on 15-Jun-2020 10:47:26

19K+ Views

To remove an element from the dictionary, we first need to check if it exists in the dictionary.We'll use the hasKey method for that. Then we can directly delete it using the delete operator.We'll return a Boolean so that the place where we call this method can know whether the ... Read More

The Keys and values method in Javascript

Samual Sam

Samual Sam

Updated on 15-Jun-2020 10:41:20

115 Views

Sometimes when working with a dictionary, we need only the keys of the dictionary as an array for some task. We can easily get the properties of an object using Object.keys. We'll use this method to return the keys from our container object. Examplekeys() {    return Object.keys(this.container); }You can test ... Read More

The Dictionary Class in Javascript

Samual Sam

Samual Sam

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

353 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) {       ... Read More

Hash Table Data Structure in Javascript

Samual Sam

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 ... Read More

When should you use sets in Javascript?

Samual Sam

Samual Sam

Updated on 15-Jun-2020 09:50:56

105 Views

Whenever you want to store unique elements in a container for which the order doesn't matter and you mainly want to use it to check for membership of different objects.Sets are also useful when you want to perform operations like union, intersection, a difference like you do in mathematical sets.Let's ... Read More

Add elements to a Set using Javascript

Samual Sam

Samual Sam

Updated on 15-Jun-2020 09:45:20

116 Views

The add method checks if a value already exists in the set, if not, then it adds that value to the set. We can implement it as follows − Exampleadd(val) {    if (!this.has(val)) {       this.container[val] = val; return true;    }    return false; }You can test ... Read More

Clearing the set using Javascript

Samual Sam

Samual Sam

Updated on 15-Jun-2020 09:39:55

93 Views

The clear method is pretty straightforward. We can just reassign the container variable to a new object and the set will now be empty. This can be implemented as follows − Exampleclear() {    this.container = {}; }You can test this using −Exampleconst testSet = new MySet(); testSet.add(1); testSet.add(2); testSet.add(5); ... Read More

Adding two Sets in Javascript

Samual Sam

Samual Sam

Updated on 15-Jun-2020 09:36:21

3K+ Views

The operation of adding 2 sets is known as a union. You need to add every object from one set to another while checking for duplicates. We can just use the 2 methods we already implemented to implement this method.We'll implement this function as a static function as we don’t ... Read More

Subtract two Sets in Javascript

Samual Sam

Samual Sam

Updated on 15-Jun-2020 09:33:39

2K+ Views

The difference of 2 sets means the set being subtracted should have all its elements removed from the set it is being subtracted from. So we can iterate over the second set and remove all the elements present in it from the first set. Examplestatic difference(s1, s2) {    if (!s1 ... Read More

Advertisements