How to get symmetric difference between two arrays in JavaScript ?
Last Updated :
05 Jul, 2023
In this article, we will see how to get the symmetric difference between two arrays using JavaScript.
In Mathematics the symmetric difference between two sets A and B is represented as A Δ B = (A – B) ∪ (B – A)
- It is defined as a set of all elements that are present in either set A or set B but not in both.
- In simple words, common elements are discarded from both sets.
For example:
A = { 1, 2, 3, 4, 5, 6}
B = { 4, 5, 6, 7 }
A - B = { 1, 2, 3, 4, 5, 6} - { 4, 5, 6, 7 }
= { 1, 2, 3 }
B - A = { 4, 5, 6, 7 } - { 1, 2, 3, 4, 5, 6}
= { 7, 1, 2, 3 }
A Δ B = ( A - B ) ∪ ( B - A )
= { 1, 2, 3 } ∪ { 7, 1, 2, 3 }
A Δ B = { 1, 2, 3, 7 }
We can achieve this with the following approaches:
Approaches to get Symmetric difference:
Approach 1: Naive method – using Javascript for loop.
Example: In this example, we will be finding the symmetric difference of the two arrays using Javascript for loop.
Javascript
const a = [1, 2, 3, 4, 5, 7, 9];
const b = [5, 6, 7, 8, 9];
const result = [];
function difference(arr1, arr2) {
let i = 0,
j = 0;
let flag = false ;
for (i = 0; i < arr1.length; i++) {
j = 0;
flag = false ;
while (j != arr2.length) {
if (arr1[i] == arr2[j]) {
flag = true ;
break ;
}
j++;
}
if (!flag) {
result.push(arr1[i]);
}
}
flag = false ;
for (i = 0; i < arr2.length; i++) {
j = 0;
flag = false ;
while (j != arr1.length) {
if (arr2[i] == arr1[j]) {
flag = true ;
break ;
}
j++;
}
if (!flag) {
result.push(arr2[i]);
}
}
return result;
}
console.log(difference(a, b));
|
Output
[ 1, 2, 3, 4, 6, 8 ]
In this approach, we 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.
Syntax:
array1.filter((element) => !array2.includes(element));
Example: In this example, we use filter() and includes() methods to get the difference between the arrays as shown:
Javascript
const array1 = [1, 2, 3, 4, 5, 7, 9];
const array2 = [5, 6, 7, 8, 9];
const difference1 = array1.filter(
(element) => !array2.includes(element));
const difference2 = array2.filter(
(element) => !array1.includes(element));
const result = [...difference1, ...difference2];
console.log(result);
|
Output
[ 1, 2, 3, 4, 6, 8 ]
Approach 3: Using Set and filter() Method
In this approach, 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.
Syntax:
const difference = [...set1].filter((element) => !set2.has(element));
Example: In this example, we will get the differences of arrays using set and filter() method and show the combined result as output:
Javascript
const array1 = [1, 2, 3, 4, 5, 7, 9];
const array2 = [5, 6, 7, 8, 9];
const set1 = new Set(array1);
const set2 = new Set(array2);
const difference1 = [...set1].filter((element) => !set2.has(element));
const difference2 = [...set2].filter((element) => !set1.has(element));
const result = [...difference1, ...difference2];
console.log(result);
|
Output
[ 1, 2, 3, 4, 6, 8 ]
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.
Syntax:
const difference = array1.reduce((result, element) => {
if (array2.indexOf(element) === -1) {
result.push(element);
}
return result;
}, []);
Example: In this example, we will get the differences of arrays using reduce() and includes() methods and show the combined result as output:
Javascript
const array1 = [1, 2, 3, 4, 5, 7, 9];
const array2 = [5, 6, 7, 8, 9];
const difference1 = array1.reduce((result, element) => {
if (array2.indexOf(element) === -1) {
result.push(element);
}
return result;
}, []);
const difference2 = array2.reduce((result, element) => {
if (array1.indexOf(element) === -1) {
result.push(element);
}
return result;
}, []);
const result = [...difference1, ...difference2];
console.log(result);
|
Output
[ 1, 2, 3, 4, 6, 8 ]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...