Skip to content
Related Articles

Related Articles

Improve Article

How to get symmetric difference between two arrays in JavaScript ?

  • Last Updated : 06 Apr, 2021

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.

Example 1:

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 }

Code :

Javascript




<script>
    /* Defining two arrays and a 
                resultant array*/
    const a = [1, 2, 3, 4, 5, 7, 9];
    const b = [5, 6, 7, 8, 9];
    const result = [];
  
    /* Defining the function with two 
    arguments array inputs */
    function difference(arr1, arr2) {
        var i = 0,
            j = 0;
        var flag = false;
  
        /* For array 1 */
        for (i = 0; i < arr1.length; i++) {
  
            /* Reseting the flag and the 
            other array iterator */
            j = 0;
            flag = false
            while (j != arr2.length) {
                if (arr1[i] == arr2[j]) {
                    flag = true;
                    break;
                }
                j++;
            }
  
            /* If value is not present in the 
            second array then push that value 
            to the resultant array */
            if (!flag) {
                result.push(arr1[i]);
            }
        }
        flag = false;
  
        /* For array 2 */
        for (i = 0; i < arr2.length; i++) {
  
            /* Reseting the flag and the 
            other array iterator */
            j = 0;
            flag = false
            while (j != arr1.length) {
                if (arr2[i] == arr1[j]) {
                    flag = true;
                    break;
                }
                j++;
            }
  
            /* If value is not present in the
            first array then push that value 
            to the resultant array */
            if (!flag) {
                result.push(arr2[i]);
            }
        }
        return result;
    }
    console.log(difference(a, b));
</script>

Output:

[ 1, 2, 3, 4, 6, 8]



My Personal Notes arrow_drop_up
Recommended Articles
Page :