Clear a Set Using JavaScript

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

182 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); testSet.display(); testSet.clear(); testSet.display();OutputThis will give the output −{ '1': 1, '2': 2, '5': 5 } { }

Loop Through a Set Using JavaScript

karthikeya Boyini
Updated on 15-Jun-2020 09:37:52

683 Views

In the set that we implemented, we can create a for each function in our class and accept a callback that we can call on every element. Let's see how we can implement such a function − ExampleforEach(callback) {    for (let prop in this.container) {       callback(prop);    } }You can test this using − Exampleconst testSet = new MySet(); testSet.add(1); testSet.add(2); testSet.add(5); testSet.forEach(elem => console.log(`Element is ${elem}`));OutputThis will give the output −Element is 1 Element is 2 Element is 5The ES6 Set API also provides the same functionality using the forEach method.

Add Two Sets in JavaScript

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 want to mutate existing sets, but create and return a new one. We first need to check if the object passed to it is really an instance of the MySet class. Examplestatic union(s1, s2) {    if (!s1 instanceof MySet || !s2 instanceof MySet) {       console.log("The given objects ... Read More

Subtract Two Sets in Javascript

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

3K+ 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 instanceof MySet || !s2 instanceof MySet) {       console.log("The given objects are not of type MySet");       return null;    }    let newSet = new MySet();    s1.forEach(elem => newSet.add(elem));    s2.forEach(elem => newSet.delete(elem));    return newSet; }You can test this using − Exampleconst testSet1 = ... Read More

The Set Class in JavaScript

Samual Sam
Updated on 15-Jun-2020 09:30:31

170 Views

Here is the complete implementation of the MySet class. Exampleclass MySet {    constructor() {       this.container = {};    }    display() {       console.log(this.container);    }    has(val) {       return this.container.hasOwnProperty(val);    }    add(val) {       if (!this.has(val)) {          this.container[val] = val;          return true;       }       return false;    }    delete(val) {       if (this.has(val)) {          delete this.container[val];          return true;       }   ... Read More

Remove Elements from a Linked List Using JavaScript

karthikeya Boyini
Updated on 15-Jun-2020 09:29:51

3K+ Views

Removing an element is very easy in a linked list. We just need to get rid of the node we want to remove, ie, lose its reference. There are 3 cases we need to consider −Removing an element from head: In this case, we can simply assign head = head.next. This way we'll lose the reference of the first element. And out head will start pointing to the second element. Removing an element from the tail: In this case, we can simply assign the node.next of second last node to be null and we'll get rid of the last element from ... Read More

The Linked List Class in JavaScript

Samual Sam
Updated on 15-Jun-2020 09:28:03

196 Views

Here is the complete implementation of the LinkedList class −  Exampleclass LinkedList {    constructor() {       this.head = null;       this.length = 0;    }    insert(data, position = this.length) {       let node = new this.Node(data);       if (this.head === null) {          this.head = node;          this.length++;          return this.head;       }       let iter = 1;       let currNode = this.head;       while (currNode.next != null && iter < position) { ... Read More

Creating a Doubly Linked List using JavaScript

Samual Sam
Updated on 15-Jun-2020 09:26:17

344 Views

Lets start by defining a simple class with a constructor that initializes the head and tail to null. We'll also define another structure on the prototype of the DoublyLinkedList class that'll represent each node in the linked list. Exampleclass LinkedList {    constructor() {       this.head = null;       this.tail = null;       this.length = 0;    } } LinkedList.prototype.Node = class {    constructor(data) {       this.data = data;       this.next = null;       this.prev = null;    } };Let's also create a display function that'll help us ... Read More

Inserting Elements to a Doubly Linked List Using JavaScript

karthikeya Boyini
Updated on 15-Jun-2020 09:24:31

562 Views

We need to create a function insert(data, position) that inserts data at given position in the linked list. We'll perform the following steps −Create a new NodeCheck if the list is empty. If it then adds the node to head and tail and return.If not, then we'll iterate to the position we want to insert it to using currElem. We iterate a linked list by making currElem equal to currElem.next. Now we change the links in the following way −Make new node point to next node in a listMake next node's previous point to the new nodeMake our node point to ... Read More

Remove Elements from a Double Linked List using JavaScript

Samual Sam
Updated on 15-Jun-2020 09:22:15

619 Views

Removing an element is very easy in a linked list. We just need to get rid of the node we want to remove, ie, lose its reference. There are 3 cases we need to consider −Removing an element from head: In this case, we can simply assign head = head.next and remove the previous link from the next element. This way we'll lose the reference of the first element. And out head will start pointing to the second element.Removing an element from the tail: In this case, we can simply assign the node.next of second last node to be null ... Read More

Advertisements