Clearing the set using Javascript


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 − 

Example

clear() {
   this.container = {};
}

You can test this using −

Example

const testSet = new MySet();

testSet.add(1);
testSet.add(2);
testSet.add(5);

testSet.display();
testSet.clear();
testSet.display();

Output

This will give the output −

{ '1': 1, '2': 2, '5': 5 }
{ }

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 15-Jun-2020

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements