Set Operations in JavaScript with Set

The sets or sets They are collections of elements that are mainly characterized by being unordered and not containing duplicate elements. In programming, set operations are fundamental tools for working with collections of data. JavaScript offers, through the object Set, a series of operations to efficiently and easily manage these sets of elements.

What is a Set in JavaScript?

Definition of Set

A Set is an object introduced in ES6 (ECMAScript 2015) that allows storing unique values of any type. Unlike an array, where the elements can be duplicates and have a specific order, the elements in an Set They are unique and their order is not significant.

Creating a Set

To create a Set In JavaScript, we simply use the constructor newSet():

let set = new Set();

We can start a Set with some values passing an iterable, like an array, to the constructor:

let numbers = new Set([1, 2, 3, 4, 5]);

Add and Delete Elements

To add elements to the set, we use the method add():

set.add(1); set.add(2);

To remove an element from the set, we use the method delete():

set.delete(1); // Remove element 1 from the set

Now, with these basic notions clear, let's delve into the set operations that we can perform using the object Set in JavaScript.

Basic Set Operations

The most common set operations are union, intersection, difference, and checking subsets. We are going to explain each of these operations and how to implement them in JavaScript using the structure Set.

Union of sets

Joining two sets results in a new set that contains all the unique elements that are in at least one of the original sets.

let a = new Set([1, 2, 3]); let b = new Set([3, 4, 5]); let union = new Set([...a, ...b]); console.log(union); // Displays {1, 2, 3, 4, 5}

Intersection of Sets

The intersection of two sets results in a new set containing only the elements that are in both sets.

let intersection = new Set([...a].filter(x => b.has(x))); console.log(intersection); // Sample {3}

Difference of Sets

The difference of two sets results in a new set that contains the elements that are in the first set but not in the second.

let difference = new Set([...a].filter(x => !b.has(x))); console.log(difference); // Displays {1, 2}

Subset Checking

Checking whether one set is a subset of another means checking whether all the elements of the first set are contained in the second.

let isSubset = [...a].every(item => b.has(item)); console.log(isSubset); // Display false

Advanced Methods and Considerations

Iteration of Elements in a Set

To iterate over all elements of a Set, we can use loops like for…of:

for (let number of numbers) { console.log(number); }

But what if we need more advanced transformations? This is where the Array methods in combination with spread operator (...) are useful.

Transformation and Organization

Given the Set does not ensure the order of its elements and lacks methods such as sort(), we generally convert the set to an array to manipulate the order:

let setNumbers = new Set([4, 1, 3]); let arrayNumbers = [...arrayNumbers].sort((a, b) => a - b); console.log(arrayNumbers); // Sample [1, 3, 4]

Set Size

To get the number of elements in a Set, we can use the property size:

console.log(numbers.size); // Sample 5 (if referring to the set created above)

Applications and Use of Sets in Programming

The sets They are especially useful when we need to ensure the uniqueness of elements, as in the following scenarios:

  • Elimination of duplicates in arrays
  • Data storage where order and repetition don't matter
  • Set operations, such as those already mentioned, in algorithms and business logic

Conclusion

Set in JavaScript is a powerful and flexible data structure that should be part of the arsenal of any developer working with data sets. With the set operations we've explored, you'll be able to manage your data collections more effectively and clearly, eliminating unnecessary complexity and improving the readability of your code.


We hope this article has given you a clear view of the object Set in JavaScript, how it is used to perform set operations and the value it can bring to your development projects. Keep practicing and experimenting with Set to take advantage of its full potential!

Facebook
Twitter
Email
Print

Leave a Reply

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

en_GBEnglish