
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 Articles
- Finding the difference between two arrays - JavaScript
- How to get the difference between two arrays in JavaScript?
- Find the compatibility difference between two arrays in C++
- Golang program to calculate the symmetric difference between two slices
- Python Program to Calculate the Symmetric Difference Between Two Lists
- How to find set difference between two Numpy arrays?
- How to find the common elements between two or more arrays in JavaScript?
- Difference between Asymmetric and Symmetric Multiprocessing
- Difference Between Symmetric and Asymmetric Multiprocessing
- Compare two arrays of single characters and return the difference? JavaScript
- Python Pandas - Compute the symmetric difference of two Index objects
- Difference between two strings JavaScript
- Finding the missing number between two arrays of literals in JavaScript
- How to find intersection between two Numpy arrays?
- JavaScript Program to Find difference between sums of two diagonals

Advertisements