Sets in Javascript

Introduction

Sets are really simple. They store unique values, which means any repeated values will be automatically removed from the set. This is really great when it comes to performance. We can definitely use lists to store data and if needed, we can remove duplicated values from a list but that requires us to iterate over a list to identify duplicate values and remove them and it costs heavily in terms of performance. Sets come to rescue for such problems.

Methods in Sets

  • new Set(iterable) - creates a new set that takes an array of values as an input.
let fruits = new Set(['apple', 'banana', 'mango']);

console.log(fruits);

/* OUTPUT: 
  Set(3) { 'apple', 'oranges', 'banana' }
 */
  • set.add(value) - The .add() method allows us to add new values to the set.
let fruits = new Set(['apple', 'banana', 'mango']);
fruits.add('oranges');

console.log(fruits);

/* OUTPUT: 
  Set(4) { 'apple', 'oranges', 'banana' , 'oranges'}
 */
  • set.has(value) - checks for the existence of a value in a set. Returns a boolean - true if the value exists and false if the value doesn't exist.
let fruits = new Set(['apple', 'banana', 'mango']);

console.log(fruits.has('apple')); //will return true
  • set.delete(value) - deletes the value from the set if it exists.
let fruits = new Set(['apple', 'banana', 'mango']);

fruits.delete('mango');

console.log(fruits);

/* OUTPUT: 
  Set(2) { 'apple', 'banana'}
 */
  • set.clear() - used to empty the set.
let fruits = new Set(['apple', 'banana', 'mango']);

fruits.clear();

console.log(fruits);

/* OUTPUT: 
Set(0) {}
 */
  • set.size - Returns the size of the set.
let fruits = new Set(['apple', 'banana', 'mango']);

console.log(fruits.size);

/* OUTPUT: 
    3
 */

Iterables in Sets

for...of, and forEach loop can be used to loop over the set.

//Using the for...of loop

let fruits = new Set(['apple', 'banana', 'mango']);

for(let fruit of fruits) {
  console.log(fruit);
}

/* OUTPUT: 
    apple
    banana
    mango
 */
//Using the forEach loop
let fruits = new Set(['apple', 'banana', 'mango']);

fruits.forEach((value, valueAgain, set) => {
  console.log(`${value}, ${valueAgain}`);
})

/* OUTPUT: 
    ['apple', 'apple']
    ['banana', 'banana']
    ['mango', 'mango']
 */

Sets use the same methods that are used for maps for iterating over it.

  • set.keys() - Returns an iterable object for values in a set
let fruits = new Set(['apple', 'banana', 'mango']);

for(let key of set.keys()) {
  console.log(key);
}

/* OUTPUT: 
    apple
    banana
    mango
 */
  • set.values() - Same as set.keys()
let fruits = new Set(['apple', 'banana', 'mango']);

for(let value of set.values()) {
  console.log(value);
}

/* OUTPUT: 
    apple
    banana
    mango
 */
  • set.entries() - Returns an iterable object for entries [value, value]
let fruits = new Set(['apple', 'banana', 'mango']);

for(let entry of set.entries()) {
  console.log(value);
}

/* OUTPUT: 
    ['apple', 'apple']
    ['banana', 'banana']
    ['mango', 'mango']
 */

Links

Below is the link, where you can find the source code related to the implementation of the above mentioned methods, and iterables in sets, in Github. Do check it out for your reference.

Demonstration of the above mentioned methods and iterables in sets in Javascript

Final Thoughts

Learning new things and consistently upgrading oneself in terms of knowledge and skills feels really great. It is a great use of time and it results in one getting better everyday in one's own career to the point where he/she ends up standing apart from others which is really rewarding to oneself. In fact, more rewarding than earning a huge sum of money while doing the job.

You can follow me on Github, and LinkedIn.

1
Subscribe to my newsletter

Read articles from Nikhil Kumar Patra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nikhil Kumar Patra
Nikhil Kumar Patra

Web Development fanatic, excited about learning anything related to the same, from the basics to the advanced. I am a final year undergraduate at the International Institute of Information Technology, Bhubaneswar. I like to solve problems and learn about complex algorithms, and new technologies, and I aspire to create an impact in the world of Computer Science someday ❤️. Apart from Web Development, I am currently exploring and learning about iOS App development.