Find set difference between two arrays in JavaScript
This post will discuss how to find the set difference between two arrays in JavaScript.
Finding the set difference between two arrays in JavaScript means finding the elements that are present in one array but not in the other. There are several ways to do this, depending on the performance, readability, and compatibility of the code. Here are some of the functions that we can use, along with some examples:
1. Using filter() and includes() functions
To find the set difference between two arrays in JavaScript, we can use the filter() function along with the includes() function. The filter() function creates a new array with the elements that pass a test implemented by a function. The function checks if each element of the first array is not included in the second array using the includes() function. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 |
function difference(arr1, arr2) { return arr1.filter(element => !arr2.includes(element)); } let arr1 = [1, 2, 3, 4]; let arr2 = [2, 4, 6, 8]; let diff = difference(arr1, arr2); console.log(diff); // [1, 3] |
In this example, arr1.filter(element => !arr2.includes(element)) filters out the elements from arr1 that are present in arr2, resulting in the set difference. This function is simple and elegant, but it may not be very efficient for large arrays, as it has a quadratic time complexity. It also requires ES6 support or a polyfill for older browsers.
2. Using a for loop with indexOf() function
This method uses a for loop to iterate over the first array and check if each element is not present in the second array using the indexOf() function. If the element is not found, it is pushed to a new array. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function difference(arr1, arr2) { let diff = []; for (let i = 0; i < arr1.length; i++) { if (arr2.indexOf(arr1[i]) === -1) { diff.push(arr1[i]); } } return diff; } let arr1 = [1, 2, 3, 4]; let arr2 = [2, 4, 6, 8]; let diff = difference(arr1, arr2); console.log(diff); // [1, 3] |
This function is more compatible with older browsers, but it is also not very efficient for large arrays, as it has a quadratic time complexity. It is also less concise and readable than the filter() function.
3. Using Set and filter() function
This method uses the Set object to store the elements of the second array in a set data structure, which allows fast lookup of values. Then it uses the filter() function to create a new array with the elements of the first array that are not in the set. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 |
function difference(arr1, arr2) { let set = new Set(arr2); return arr1.filter(x => !set.has(x)); } let arr1 = [1, 2, 3, 4]; let arr2 = [2, 4, 6, 8]; let diff = difference(arr1, arr2); console.log(diff); // [1, 3] |
This function is more efficient than the previous functions, as it has a linear time complexity. However, it also requires ES6 support or a polyfill for older browsers. It also assumes that the arrays do not contain duplicate elements.
4. Using reduce() and includes() functions
This method uses the reduce() function to accumulate a new array with the elements of the first array that are not included in the second array using the includes() function. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function difference(arr1, arr2) { return arr1.reduce((acc, x) => { if (!arr2.includes(x)) { acc.push(x); } return acc; }, []); } let arr1 = [1, 2, 3, 4]; let arr2 = [2, 4, 6, 8]; let diff = difference(arr1, arr2); console.log(diff); // [1, 3] |
This function is similar to the filter() function in terms of simplicity and elegance, but it may be slightly slower due to the overhead of creating and returning an accumulator array. It also requires ES6 support or a polyfill for older browsers.
5. Using Lodash or Underscore Library
Another option is to use a third-party libraries like Lodash or Underscore. This function is convenient and easy to use, as it provides built-in functions for finding the set difference between two arrays. For example, to find the set difference between two arrays, we can do like below. This returns an array containing the elements of first array that are not in second array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// import the library const _ = require("lodash"); // or const _ = require("underscore"); function difference(arr1, arr2) { let diff = _.difference(arr1, arr2); return diff; } let arr1 = [1, 2, 3, 4]; let arr2 = [2, 4, 6, 8]; let diff = difference(arr1, arr2); console.log(diff); // [1, 3] |
That’s all about finding the set difference between two arrays 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 :)