- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

# Find the Symmetric difference between two arrays - JavaScript

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]

## Example

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));

## Output

This will produce the following output in console −

[2, 4, 9]

- 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
- Difference between two strings JavaScript
- Python Pandas - Compute the symmetric difference of two Index objects
- How to find the common elements between two or more arrays in JavaScript?
- How to find intersection between two Numpy arrays?
- Compare two arrays of single characters and return the difference? JavaScript
- Find the difference between two datetime values with MySQL?
- Python Pandas – Find the Difference between two Dataframes
- Finding the missing number between two arrays of literals in JavaScript
- Program to find uncommon elements in two arrays - JavaScript

Advertisements