- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 8894 Articles for Front End Technology
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
474 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.
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
94 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 } { }
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
108 Views
The delete method checks if a value already exists in the set, if it does, then it removes that value from the set. We can implement it as follows &minusl Exampledelete(val) { if (this.has(val)) { delete this.container[val]; return true; } return false; }You can test this using − Exampleconst testSet = new MySet(); testSet.add(1); testSet.add(2); testSet.add(5); testSet.delete(5); testSet.delete(2); testSet.display(); console.log(testSet.has(5)); console.log(testSet.has(20)); console.log(testSet.has(1));OutputThis will give the output −{ '1': 1} False False TrueIn ES6, you use the delete function as follows − Exampleconst testSet = new MySet(); testSet.add(1); testSet.add(2); testSet.add(5); testSet.delete(5); ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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 this using − Exampleconst testSet = new MySet(); testSet.add(1); testSet.add(2); testSet.add(5); testSet.add(2); testSet.display(); console.log(testSet.has(5)); console.log(testSet.has(20)); console.log(testSet.has(1));OutputThis will give the output −{ '1': 1, '2': 2, '5': 5 } True False TrueNote that even though we tried adding 2 twice, it only got added once. If you try logging ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
419 Views
Let's create a MySet class so that it doesn't hide the actual set class in JS. We'll create a container object that'll keep track of all our values that we add to the set. We'll also create a display function that prints the set for us. Exampleclass MySet { constructor() { this.container = {}; } display() { console.log(this.container); } }In ES6, you can directly create a set using the Set class. For example, Exampleconst set1 = new Set(); const set2 = new Set([1, 2, 5, 6]);Checking for membershipThe has method checks ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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 look at both how we can define our own set and use the existing one in ES6.Methods we'll implementThe ES6 set API provides some methods. We'll implement these methods in our implementation and also look at how to use them using the built-in class.add() − Adds a new element to the ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
400 Views
A set is an abstract data type that can store certain values, without any particular order and no repeated values. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set.
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
146 Views
In the doubly linked list, the next pointer of the last node points to the first node and the previous pointer of the first node points to the last node making the circular in both directions.Insertions and deletions in a circular linked list are the same as other linked lists. You just need to keep track of the last link while performing operations on either end of the linked list.You can look up and try implementing Circular linked list using Circular Linked List Algorithm as a guide.