Open In App

Difference Between Two Arrays in JavaScript?

Last Updated : 12 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

These are the following ways to Get the Difference Between Two Arrays in JavaScript:

1. Using the filter() and includes() Methods – Mostly Used

We can use the filter() method on the first array and check if each item is not present in the second array using the includes() method. The resulting array contains the elements that are present in the first array but not in the second array.

JavaScript
let a1 = [1, 2, 3, 4, 5];
let a2 = [3, 4, 5, 6, 7];

let res = a1.filter((e) => !a2.includes(e));

console.log(res); 

Output
[ 1, 2 ]

2. Using for Loop and indexOf() Method

To find the difference between two arrays in JavaScript, you can iterate over one array using a for loop and check if each element exists in the other array using the indexOf() method. If an element is not found, it is added to the result array.

JavaScript
let a1 = [1, 2, 3, 4, 5, 0];
let a2 = [3, 4, 5, 6, 7, 9];
let res = [];

for (let i = 0; i < a1.length; i++) {
    if (a2.indexOf(a1[i]) === -1) {
        res.push(a1[i]);
    }
}
console.log(res);

Output
[ 1, 2, 0 ]

3. Using Set and filter() Method

The arrays are converted to Sets using the Set constructor. The Set data structure only allows unique values. By filtering out the elements from the first set that also exist in the second Set, we can obtain the difference.

JavaScript
let a1 = [1, 2, 3, 4, 5];
let a2 = [3, 4, 5, 6, 7];

let s1 = new Set(a1);
let s2 = new Set(a2);

let res = [...s1].filter(
    (e) => !s2.has(e));

console.log(res);

Output
[ 1, 2 ]

4. Using reduce() and includes() Methods

This method iterates over the first array and checks if each element is present in the second array using the includes() method. It accumulates the elements that are not found in the second array, resulting in the difference between the two arrays.

JavaScript
let a1 = [1, 2, 3, 4, 5];
let a2 = [3, 4, 5, 6, 7];

let res = a1.reduce((r, e) => {
    if (a2.indexOf(e) === -1) {
        r.push(e);
    }
    return r;
}, []);

console.log(res); 

Output
[ 1, 2 ]

5. Using Lodash _.difference() Function

Lodash _.difference() function is used to remove a single element or the array of elements from the original array. This function works pretty much the same as the core function of JavaScript i.e. filter.

JavaScript
let _ = require("lodash");
let a1 = [1, 2, 3];
let a2 = [2, 3]
let res = _.difference(a1, a2);
console.log(res);

Output:

[1]

6. Using Array.filter() and Array.every() Methods

We are using the filter() method along with the every() method. We iterate over each element of the first array and check if it exists in the second array using the every() method. If an element is not found in the second array, it is included in the resulting array.

JavaScript
let a1 = [1, 2, 3, 4, 5];
let a2 = [3, 4, 5, 6, 7];

let res = a1.filter((e) => a2.every((val) => val !== e));

console.log(res);

Output
[ 1, 2 ]

7. Using Array find() and indexOf() Methods

This approach utilizes the find() method along with the indexOf() method to efficiently find elements in one array that are not present in the other array.

JavaScript
let a1 = [1, 2, 3, 4, 5];
let a2 = [3, 4, 5, 6, 7];
let res = a1.filter((e) => a2.indexOf(e) === -1);
console.log(res); 

Output
[ 1, 2 ]


Similar Reads