JavaScript – Set – DEV Community

I’m at present partaking within the #100DaysOfCode problem, and on a regular basis I attempt to resolve one Knowledge Construction and Algorithm downside utilizing JavaScript. I’ve encountered Set and Map and I wish to discuss somewhat a bit concerning the Set object sort.


The Set object sort was launched with ECMAScript 2015 (or ES6) which let you retailer all sort of JavaScript knowledge sorts as a novel set of components. It is extremely just like an Array in JavaScript nevertheless it has different syntax that means that you can entry, verify, and delete components within the Set. This explicit object sort comes with its personal strategies, particularly, the .has() technique which lets you verify for components contained in the Set very similar to Array.prototype.consists of, and in response to the MDN Docs it’s faster than .consists of on common.

The strategies that we will use with Set object sort are .add(), .clear(), .delete(), and .has(). It additionally has a property .measurement which has similarities to Array.prototype.size.

Instance: Let’s take a look at a number of the Set strategies.

const twiceTwice = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5];

const uniqueNumbers = new Set(twiceTwice);

console.log(uniqueNumbers) // { 1, 2, 3, 4, 5 };

// can add new components, any knowledge sort
uniqueNumbers.add({title: "Tanwa", favouriteColor: "Crimson"});
/* Set(7) {1, 2, 3, 4, 5, …}
0: 1
1: 2
2: 3
3: 4
4: 5
5: "Rasengan"
worth: {title: 'Tanwa', favouriteColor: 'Crimson'}
measurement: 7 */

// verify for a component
uniqueNumbers.has("Rasengan"); // True

// delete a component
uniqueNumbers.delete(2); // True

// verify measurement
uniqueNumbers.measurement; // 6

// clear

Enter fullscreen mode

Exit fullscreen mode

Instance use case

To resolve 3. Longest Substring Without Repeating Characters on leetcode, I used Set to type and retailer the substring as I iterate via the string utilizing two pointers, and every iteration, checking the Set utilizing .has() to see if the ingredient exist already earlier than utilizing .add() to append new values or .delete() to take away the ingredient ranging from the left. You’ll be able to see my resolution on my website.


Set is an object sort that means that you can retailer distinctive components of any sort. And these are the strategies:

  • .add(): you’ll be able to add a brand new ingredient to the tip of the Set. Returns the brand new Set.
  • .clear(): you’ll be able to take away all the weather within the Set.
  • .delete(): you’ll be able to take away a particular worth from the Set. Returns false if a component isn’t within the Set. Returns true for efficiently eradicating the ingredient.
  • .has(): you’ll be able to verify if a component is within the Set. Returns a Boolean.

Subsequent time you might want to take away a particular ingredient from a novel set of values, you’ll be able to attempt utilizing Set and its strategies.

Thanks for studying, I hope you discovered it helpful. Please depart feedback and feedbacks if in case you have them 🙂

Add a Comment

Your email address will not be published. Required fields are marked *