- Related Questions & Answers
- Finding the difference between two arrays - JavaScript
- Find the compatibility difference between two arrays in C++
- How to get the difference between two arrays in JavaScript?
- How to find set difference between two Numpy arrays?
- Difference between Asymmetric and Symmetric Multiprocessing
- Difference Between Symmetric and Asymmetric Multiprocessing
- How to find the common elements between two or more arrays in JavaScript?
- Difference between two strings JavaScript
- How to find intersection between two Numpy arrays?
- Find the difference between two datetime values with MySQL?
- Compare two arrays of single characters and return the difference? JavaScript
- Finding the missing number between two arrays of literals in JavaScript
- Find the difference between two timestamps in days with MySQL
- Program to find uncommon elements in two arrays - JavaScript
- Get the property of the difference between two objects in JavaScript

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In Mathematics, the symmetric difference of two sets, say A and B is represented by A △ B

And it is defined as the set of all those elements which belongs either to A or to B but not to both.

For example −

const A = [1, 2, 3, 4, 5, 6, 7, 8]; const B = [1, 3, 5, 6, 7, 8, 9];

Then the symmetric difference of A and B will be −

const diff = [2, 4, 9]

Following is the code −

const A = [1, 2, 3, 4, 5, 6, 7, 8]; const B = [1, 3, 5, 6, 7, 8, 9]; const symmetricDifference = (arr1, arr2) => { const res = []; for(let i = 0; i < arr1.length; i++){ if(arr2.indexOf(arr1[i]) !== -1){ continue; }; res.push(arr1[i]); } for(let i = 0; i < arr2.length; i++){ if(arr1.indexOf(arr2[i]) !== -1){ continue; }; res.push(arr2[i]); }; return res; }; console.log(symmetricDifference(A, B));

This will produce the following output in console −

[2, 4, 9]

Advertisements