Last updated: Dec 27, 2022
Reading timeยท5 min
To get a union of two arrays:
Set()
constructor to remove the
duplicates.Set
back to an array.const arr1 = ['a', 'b', 'c']; const arr2 = ['a', 'b', 'c', 'd']; const union = Array.from(new Set([...arr1, ...arr2])); // ๐๏ธ ['a', 'b', 'c', 'd'] console.log(union);
Set
objects is the smallest Set
which contains all the elements of both Set
objects.We used the Set() constructor to remove all of the duplicates from the two arrays.
Set
objects only store unique values, so all of the duplicates get
automatically removed.
If we pass it an array containing the same value multiple times to the Set()
constructor, it would only get added once to the Set
.
console.log(new Set(['a', 'a', 'a'])); // ๐๏ธ { 'a' }
We used the spread syntax (...) to unpack the values of the two arrays into a third array.
const arr1 = ['a', 'b', 'c']; const arr2 = ['a', 'b', 'c', 'd']; const union = Array.from(new Set([...arr1, ...arr2])); // ๐๏ธ ['a', 'b', 'c', 'd'] console.log(union);
The last step is to convert the set back to an array, using the Array.from method.
You can also use the spread syntax (...) to convert the Set
object to an
array, but it makes the code more difficult to read.
const arr1 = ['a', 'b', 'c']; const arr2 = ['a', 'b', 'c', 'd']; const union = [...new Set([...arr1, ...arr2])]; // ๐๏ธ ['a', 'b', 'c', 'd'] console.log(union);
Alternatively, you can use the Array.filter()
method.
Array.filter()
This is a two-step process:
const arr1 = ['a', 'b', 'c']; const arr2 = ['a', 'b', 'c', 'd']; function getUnion(array1, array2) { const difference = array1.filter( element => !array2.includes(element) ); return [...difference, ...array2]; } // ๐๏ธ ['a', 'b', 'c', 'd'] console.log(getUnion(arr1, arr2));
We used the Array.filter method to iterate over the first array.
On each iteration, we check if the current element is not contained in the second array.
The output of the filter()
method is a new array that contains the elements of
the first array that are not in the second array.
const arr1 = ['a', 'b', 'c', 'd', 'e', 'f']; const arr2 = ['a', 'b', 'c', 'd']; const difference = arr1.filter(element => !arr2.includes(element)); console.log(difference); // ๐๏ธ [ 'e', 'f' ]
The last step is to merge the difference with the values of the second array.
function getUnion(array1, array2) { const difference = array1.filter(element => !array2.includes(element)); return [...difference, ...array2]; } const arr1 = ['a', 'b', 'c', 'd', 'e', 'f']; const arr2 = ['a', 'b', 'c', 'd']; // ๐๏ธ [ 'e', 'f', 'a', 'b', 'c', 'd' ] console.log(getUnion(arr1, arr2));
This approach is not as direct and intuitive as leveraging the Set
functionality to remove the duplicates.
lodash
Alternatively, you can use the lodash
library.
lodash
library.npm install lodash # ๐๏ธ only if you use TypeScript npm install @types/lodash --save-dev
union()
method to get the union of two or more arrays.import _ from 'lodash'; const arr1 = ['a', 'b', 'c', 'd', 'e']; const arr2 = ['a', 'e', 'f']; const union = _.union(arr1, arr2); // ๐๏ธ [ 'a', 'b', 'c', 'd', 'e', 'f' ] console.log(union);
The union()
method creates an array of unique values, in order, from all of
the provided arrays.
You can also use the union()
method to get the union of more than two arrays.
import _ from 'lodash'; const arr1 = ['a', 'b', 'c', 'd', 'e']; const arr2 = ['a', 'e', 'f']; const arr3 = ['e', 'y']; const union = _.union(arr1, arr2, arr3); // ๐๏ธ [ // 'a', 'b', 'c', // 'd', 'e', 'f', // 'y' // ] console.log(union);
We passed 3 arrays to the union()
method and it returned the union of the
arrays.
To get a union of two Set
objects:
Set()
constructor.Set
will contain the union of the other two.const set1 = new Set(['a', 'b', 'c']); const set2 = new Set(['a', 'b', 'd']); const union = new Set([...set1, ...set2]); console.log(union); // ๐๏ธ {'a', 'b', 'c', 'd'}
We used the
spread syntax (...) to
unpack the values of the two Set
objects into an array.
const set1 = new Set(['a', 'b', 'c']); const set2 = new Set(['a', 'b', 'd']); const arr = [...set1, ...set2]; console.log(arr); // ๐๏ธ ['a', 'b', 'c' ,'a', 'b', 'd']
The array stores duplicate values as opposed to a Set
. Once the array is
passed to the Set()
constructor, all of the duplicates are ignored.
const set1 = new Set(['a', 'b', 'c']); const set2 = new Set(['a', 'b', 'd']); const union = new Set([...set1, ...set2]); console.log(union); // ๐๏ธ {'a', 'b', 'c', 'd'}
The Set() constructor takes an iterable object as a parameter, so an array is perfect.
You can use this approach to get the union of more than two Set objects.
const set1 = new Set(['a', 'b', 'c']); const set2 = new Set(['a', 'b', 'd']); const set3 = new Set(['a', 'b', 'e']); const union = new Set([...set1, ...set2, ...set3]); console.log(union); // ๐๏ธ Set(5) { 'a', 'b', 'c', 'd', 'e' }
You can unpack as many Set
objects as necessary in the call to the Set()
constructor.
for...of
loopThis is a four-step process:
Set
to the Set()
constructor to create a third Set
.for...of
loop to iterate over the second Set
.Set
.Set
will contain the union of the other two.function getUnion(setA, setB) { const union = new Set(setA); for (const element of setB) { union.add(element); } return union; } const set1 = new Set(['a', 'b', 'c']); const set2 = new Set(['a', 'b', 'd']); console.log(getUnion(set1, set2)); // ๐๏ธ {'a', 'b', 'c', 'd'}
We created a reusable function that returns the union of two Sets.
The first step is to create a new Set
from the first Set
.
The for...of statement is
used to loop over iterable objects like arrays, strings, Map
, Set
and
NodeList
objects and generators
.
On each iteration, we use the Set.add()
method to add the current element to
the new Set
.
The Set.add method inserts a new element
with the supplied value into a Set
object, if the value is not already in the
Set
.
Sets don't store duplicate values, so we don't have to worry about handling that.
You can learn more about the related topics by checking out the following tutorials: