TypeScript Set

Learn via video courses
Topics Covered

Overview

By the word set, we get an idea that it is used for "storing unique elements" , it is a newly introduced data structure added in the versions of javascript. They are similar to maps. Each value in a set only occurs once , sets are mainly used for storing distinct data values and are not only available in typescript but are also available in c++, java, etc. Now let's read in detail about sets.

Introduction

As we already know that the set data structure in typescript is used to store distinct elements , maps, and sets have a common factor in between but typescript set are used to store only the keys while maps are used to store key-value pairs. Now that we have read about sets so let's see the syntax of how to implement them in our program :

Syntax :

This is how we can implement sets in our program.

Create set :

We can create a set by using this line in our program :

Now let's read about the various set methods in details :

Set Methods

Now we will read about different methods required in typescript sets :

Methods nameDescription of the method
set.add(val)By the word add(val) we can clearly understand that this property is used to add values in set
set.has(val)The has(val) method checks that the value which is passed in the function is either present in the function or not if it is present in the function it returns true or else it returns false.
set.delete()By the word delete we get a basic idea that this method is used to delete the values from the set.
set.clear()This method is used to delete or remove all the elements from the set.
set.size()This method is used to determine the size of the set.

These are the various set methods that we will require in typescript.

Set Method Examples

Now that we have read about the various set methods now let's see how to implement those methods in the typescript set program.

A. Typescript Program That Creates a Set and Adds Values to It

Example :

Output :

In the above code, we have created a set with employeeid as an object , the elements 131, 078, 54, and 49 are added to it and this is how the set.add(val) works. Run the above code in your editor for a better and clear explanation.

B. Typescript Program That Creates a Set and Adds Values to It Using add() Method Chaining

Input :

Output :

In this program we have created an object named set called employeeid and added the elements to the set using the add() channing method and after running the program we will get all the details of the set. Run this code in your editor for a better and clear explanation.

C. Typescript Program That Creates a Set and Checks Whether a Particular Value is Present in It

Input :

Output :

In this code we have created an object named set and have similarly added elements to then compared the values with the set and if they were present print true or else print false. Run this code in your editor for a better and clear explanation.

D. Typescript Program That Creates a Set and Finds the Size of the Set

Input :

In the above code, we have created an object named set and then added values in it, and printed the size of the set. Run the above code in your editor for a better and clear explanation.

E. Typescript Program That Creates a Set and Removes a Particular Element from It

Example :

In the above code first, we created an object named set and then added values in it and then printed the size of the set and then deleted one of the elements of the set, and then printed the updated size and the updated elements of the set. Run this code in your editor for a better and clear explanation.

F. Typescript Program That Creates a Set and Removes All the Elements from It

Input :

In the above code, we have created an object named set and then added values in it, and then printed the elements of the set after that using the clear() method deleted all the elements of the set and then printed the updated set size and updated set elements. Run the above code in your editor for a better and clear explanation.

Chaining of Set Methods

In typescript we can add elements using the add() chaining mmethod. Let's look at an example below to see how this works :

Output :

We simply added the elements using the add() chaining method and then printed the values which were present in the set. Implement the above code yourself once for a better explanation.

Now let's read about iterating set data :

Iterating Set Data

We can iterate over the set values using the for loop. Now let's see an example of how to implement it in a typescript set :

Output :

Run this code in your editor for a better and clear explanation.

Add, Retrieve and Delete Values from the Set

We have earlier read about these topics in our article now let's see an example for each of them :

Run the above code in your editor for a better and clear explanation.

Iterating over a Set

We can use the for loop in the typescript set to iterate over set values. Let's see the code implementation now :

We have simply iterated over the for set elements using them for a loop. Run the code in your editor for a better explanation.

Conclusion

  • The set data structure in the typescript set is used to store distinct elements.
  • There are various inbuilt set methods that we can perform while using the typescript set data structure.
  • We can also add elements in the set by the add() chaining method.
  • Sets are similar to maps but sets are used to store only keys while maps store the key-value pairs also.
  • In typescript, we can iterate the sets using the for loop.