Posted on: January 06, 2024|Amrish Kushwaha

How to perform union, intersection, and difference of arrays in Javascript?

How to perform union, intersection, and difference of arrays in Javascript?

Arrays are part of a developer's life. As a developer, you would have to deal with these types of data every day.

Daily, you may have to deal with multiple types of data and one of the data types is array. Sometimes you have to deal with multiple arrays together and generate a new array.

Some of the operations that you need to perform are as follows:

  • Union of arrays
  • Intersection of arrays
  • Difference between arrays (Difference of arrays from left to right, Difference of arrays from right to left)

In this article, I am going to write an algorithm to perform such operations using Javascript and those solutions will be applicable to any number of arrays.

First thing first,

Let's describe these operations. Suppose we have two arrays such as array1 and array2.

const array1 = ["Item1", "Item2", "Item3"];
const array2 = ["Item2", "Item3", "Item4", "Item5"];
// Intersection Of Arrays
const intersection = ["Item2", "Item3"];
// Union Of Arrays
const union = ["Item1", "Item2", "Item3", "Item4", "Item5"];
// Left To Right Difference
const diffLeftToRight = ["Item2"];
// Right To Left Difference
const diffRightToLeft = ["Item4", "Item5"];

Operations:

Union of Arrays:

A Union of arrays is an array that contains all the items from both arrays and it comes one thing at a time.

Here is the code to calculate the union of arrays:

function union() {
return Object.values(arguments)?.reduce((acc, currentValue) => {
return Array.from(new Set([...acc, ...currentValue]));
})
}
const arr1 = ["Item1", "Item2", "Item3"];
const arr2 = ["Item2", "Item3", "Item4", "Item5"];
const arr3 = ["Item5", "Item6", "Item7", "Item8"];
const result = union(arr1, arr2, arr3);
console.log(result); // ['Item1', 'Item2', 'Item3', 'Item4', 'Item5', 'Item6', 'Item7', 'Item8']

Intersection of Arrays:

An intersection of arrays is another array that contains only those elements that are present in both arrays.

For example, to calculate the intersection of arrays, we have to do this:

function intersection() {
return Object.values(arguments)?.reduce((acc, currentValue) => {
return acc.filter(res => currentValue?.includes(res));
})
}
const arr1 = ["Item1", "Item2", "Item3"];
const arr2 = ["Item2", "Item3", "Item4", "Item5"];
const arr3 = ["Item2", "Item4", "Item6", "Item7"];
const result = intersection(arr1, arr2, arr3);
console.log(result); // ["Item2"]

Difference of Arrays (Left To Right):

The difference between arrays is an array that contains items from the very first array that aren’t present in the rest of the arrays.

For example, difference of arrays from left to right:

function diffLeftToRight() {
return Object.values(arguments)?.reduce((acc, currentValue) => {
return acc.filter(res => !currentValue?.includes(res));
})
}
const arr1 = ["Item1", "Item2", "Item6"];
const arr2 = ["Item2", "Item3", "Item4", "Item5"];
const arr3 = ["Item2", "Item3", "Item6"];
const result = diffLeftToRight(arr1, arr2, arr3);
console.log(result); // ['Item1']

Difference of Arrays (Right To Left):

The difference between arrays is an array that contains items from the very last array which isn't present in other arrays.

For example, difference of arrays from right to left:

function diffRightToLeft() {
return Object.values(arguments)?.reduceRight((acc, currentValue) => {
return acc?.filter(res => !currentValue?.includes(res));
})
}
const arr1 = ["Item1", "Item2", "Item3"];
const arr2 = ["Item2", "Item3", "Item4", "Item5"];
const arr3 = ["Item1", "Item3", "Item5", "Item7"];
const result = diffRightToLeft(arr1, arr2, arr3);
console.log(result); // ['Item7']

Real-Life Use of Arrays:

If you want to find deleted projects from two array sets - the first array only contains active projects and the second array contains few active and all deleted projects, then the array difference would be helpful to find out the deleted projects.

If the first array only contains active projects and the second array contains active and deleted projects, then the intersection of the array would help find the deleted projects.

That's all. I hope that you would have found this article helpful.

About author:

Amrish Kushwaha

Amrish Kushwaha

I am Amrish Kushwaha. Software Engineer, Maker, Writer. I am currently focused on frontend development with curiosity of building end to end software system. Currently I am working at Rafay Systems. I love to write as well as build side projects when I am free.

Related category articles: