Merge Multiple Sets in JavaScript
This post will discuss how to merge multiple sets in JavaScript.
Merging sets means creating a new set that contains all the values from the original sets, without any duplicates. There are several methods to merge two sets in JavaScript, depending on whether we want to create a new set that contains the union of the two sets, or modify one of the existing sets by adding the elements of the other set. Here are some of the common functions:
1. Using spread operator
One way to merge multiple sets in JavaScript is to use the spread syntax (…) to expand the elements of multiple sets into an array, and then pass the array as an argument to the Set() constructor. This will create a new set that contains all the unique values from the original sets. For instance, we can use the spread operator to merge three sets like this.
|
1 2 3 4 5 6 7 8 9 10 |
// Create three sets const set1 = new Set([1, 2, 3]); const set2 = new Set([4, 5, 6]); const set3 = new Set([7, 8, 9]); // Create a new set that contains the union of all three sets const mergedSet = new Set([...set1, ...set2, ...set3]); // Set(9) { 1, 2, 3, 4, 5, 6, 7, 8, 9 } console.log(mergedSet); |
This function is simple and concise, but it may not be very efficient for large sets, as it creates a temporary array that contains all the elements of both sets before creating the new set.
2. Using a loop
We can use a loop to iterate over one or more sets and use the Set.add() function to add each element to another set. This way, we can modify an existing set by adding the elements of another set to it. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 |
// Create two sets const set1 = new Set([1, 2, 3]); const set2 = new Set([4, 5, 6]); // Add the elements of set2 to set1 for (let element of set2) { set1.add(element); } // Set(6) { 1, 2, 3, 4, 5, 6 } console.log(set1); |
This function does not create a temporary array or a new set, but it mutates one of the original sets. We can also use this function to merge more than two sets by using nested loops or spread operator. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Create three sets const set1 = new Set([1, 2, 3]); const set2 = new Set([4, 5, 6]); const set3 = new Set([7, 8, 9]); // Add the elements of set2 and set3 to set1 for (let element of [set2,set3]) { for (let item of element) { set1.add(item); } } // or use spread operator // for (let element of [...set2, ...set3]) { // set1.add(element); // } // Set(9) { 1, 2, 3, 4, 5, 6, 7, 8, 9 } console.log(set1); |
3. Using forEach() function
Another way to merge multiple sets in JavaScript is to use the Set.forEach() function to iterate over the elements of one set and add them to another set using the Set.add() function. This will modify the existing set by adding the new elements to it. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// Create three sets const set1 = new Set([1, 2, 3]); const set2 = new Set([4, 5, 6]); const set3 = new Set([7, 8, 9]); // add the elements of set2 to set1 using forEach and add methods set2.forEach(set1.add, set1); // add the elements of set3 to set1 using forEach and add methods set3.forEach(set1.add, set1); // Set(9) { 1, 2, 3, 4, 5, 6, 7, 8, 9 } console.log(set1); |
That’s all about merging multiple sets in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)