Skip to content
Related Articles

Related Articles

Symmetric difference of two sorted array
  • Difficulty Level : Basic
  • Last Updated : 20 Dec, 2019

There are two sorted array arr1 and arr2. We have to find the symmetric difference of Aarr1 and arr2. Symmetric Difference basically contains all elements of two arrays except common elements.

Symmetric difference of two array is the all 
array elements of both array except the elements 
that are presents in both array.
SymmDiff = (arr1 - arr2) UNION (arr2 - arr1). 
               OR
SymmDiff = (arr1 UNION arr2) - (arr1 INTERSECTION arr2).

Examples:

Input : arr1[] = {2, 4, 5, 7, 8, 10, 12, 15}.
        arr2[] = {5, 8, 11, 12, 14, 15}.
Output : 2 4 7 10 11 14        
        arr1[] - arr2[] = {2, 4, 7, 10}.
        arr[2] - arr1[] = {11, 14}.
        SymmDiff = (arr1[] - arr2[]) UNION 
                   (arr2[] - arr1[]).
                 = {2, 4, 7, 10, 11, 14}.


Input : arr1[] = {1, 3, 5, 8, 15, 27, 35}.
        arr2[] = {5, 7, 8, 11, 15, 18, 35}.
Output : 1 3 7 11 18 27
        arr1[] - arr2[] = {1, 3, 27}.
        arr[2] - arr1[] = {7, 11, 18}.
        SymmDiff = (arr1[] - arr2[]) UNION 
                   (arr2[] - arr1[]).
                 = {1, 3, 7, 11, 18, 27}.

A Simple Solution is to traverse through both arrays one by one. For every element of one array, check if it is present in other array. If yes, then ignore it, else print it. Time complexity of this solution is O(n*n)

An Efficient solution for find the symmetric difference of two sorted arrays is similar to merge process of merge sort. We traverse both arrays simultaneously and print smaller element if current two elements do not match and move ahead in array with smaller element. Else we ignore the elements and move ahead in both arrays.

C++




// C++ program to find the symmetric difference
// of two sorted array.
#include<iostream>
using namespace std;
void symmDiff(int arr1[], int arr2[], int n, int m)
{
    // Traverse both arrays simultaneously.
    int i = 0, j = 0;
    while (i < n && j < m)
    {
        // Print smaller element and move 
        // ahead in array with smaller element
        if (arr1[i] < arr2[j])
        {
            cout << arr1[i]<<" ";
            i++;
        }
        else if (arr2[j] < arr1[i])
        {
            cout << arr2[j]<<" ";
            j++;
        }
  
        // If both elements same, move ahead
        // in both arrays.
        else
        {
            i++;
            j++;
        }
    }
}
  
// Driver code
int main()
{
    int arr1[] = {2, 4, 5, 7, 8, 10, 12, 15};
    int arr2[] = {5, 8, 11, 12, 14, 15};
    int n = sizeof(arr1)/sizeof(arr1[0]);
    int m = sizeof(arr2)/sizeof(arr2[0]);
    symmDiff(arr1, arr2, n, m);
    return 0;
}


Java




// Java program to find the symmetric
// difference of two sorted array.
import java.util.*;
class GFG
{
static void symmDiff(int[] arr1, int[] arr2,
                           int n, int m)
{
    // Traverse both arrays simultaneously.
    int i = 0, j = 0;
    while (i < n && j < m)
    {
        // Print smaller element and move 
        // ahead in array with smaller element
        if (arr1[i] < arr2[j])
        {
            System.out.print(arr1[i]+" ");
            i++;
        }
        else if (arr2[j] < arr1[i])
        {
            System.out.print(arr2[j]+" ");
            j++;
        }
  
        // If both elements same, move ahead
        // in both arrays.
        else
        {
            i++;
            j++;
        }
    }
}
  
// Driver code
public static void main(String[] args)
{
    int[] arr1 = {2, 4, 5, 7, 8, 10, 12, 15};
    int[] arr2 = {5, 8, 11, 12, 14, 15};
    int n = arr1.length;
    int m = arr2.length;
    symmDiff(arr1, arr2, n, m);
}
}
/* This code is contributed by Kriti Shukla */


Python3




# Python3 program to 
# find the symmetric 
# difference of two
# sorted array.
def symmDiff(arr1, arr2, n, m):
  
    # Traverse both arrays
    # simultaneously.
    i = 0
    j = 0
    while (i < n and j < m):
      
        # Print smaller element
        # and move ahead in 
        # array with smaller 
        # element
        if (arr1[i] < arr2[j]):
            print(arr1[i], end = " ")
            i+=1
          
        elif (arr2[j] < arr1[i]):
            print(arr2[j], end = " ")
            j+=1
          
        # If both elements
        # same, move ahead
        # in both arrays.
        else:
          
            i+=1
            j+=1
  
# Driver code
arr1 = [2, 4, 5, 7, 8, 10, 12, 15]
arr2 = [5, 8, 11, 12, 14, 15]
n = len(arr1)
m = len(arr2)
  
symmDiff(arr1, arr2, n, m)
  
# This code is contributed by Smitha Dinesh Semwal


C#




// C# program to find the symmetric
// difference of two sorted array.
using System;
  
class GFG {
      
    static void symmDiff(int[] arr1, int[] arr2,
                            int n, int m)
    {
          
        // Traverse both arrays simultaneously.
        int i = 0, j = 0;
          
        while (i < n && j < m)
        {
              
            // Print smaller element and move 
            // ahead in array with smaller element
            if (arr1[i] < arr2[j])
            {
                Console.Write(arr1[i]+" ");
                i++;
            }
            else if (arr2[j] < arr1[i])
            {
                Console.Write(arr2[j]+" ");
                j++;
            }
      
            // If both elements same, move ahead
            // in both arrays.
            else
            {
                i++;
                j++;
            }
        }
    }
      
    // Driver code
    public static void Main()
    {
        int[] arr1 = {2, 4, 5, 7, 8, 10, 12, 15};
        int[] arr2 = {5, 8, 11, 12, 14, 15};
        int n = arr1.Length;
        int m = arr2.Length;
          
        symmDiff(arr1, arr2, n, m);
    }
}
  
/* This code is contributed by vt_m*/


PHP




<?php
// PHP program to find the 
// symmetric difference
// of two sorted array.
function symmDiff($arr1, $arr2
                       $n, $m)
{
      
    // Traverse both arrays
    // simultaneously.
    $i = 0; $j = 0;
    while ($i < $n && $j < $m)
    {
          
        // Print smaller element
        // and move ahead in array 
        // with smaller element
        if ($arr1[$i] < $arr2[$j])
        {
            echo($arr1[$i] . " ");
            $i++;
        }
        else if ($arr2[$j] < $arr1[$i])
        {
            echo($arr2[$j] . " ");
            $j++;
        }
  
        // If both elements same, 
        // move ahead in both arrays
        else
        {
            $i++;
            $j++;
        }
    }
}
  
// Driver code
$arr1 = array(2, 4, 5, 7, 8, 10, 12, 15);
$arr2 = array(5, 8, 11, 12, 14, 15);
$n = sizeof($arr1);
$m = sizeof($arr2);
symmDiff($arr1, $arr2, $n, $m);
  
// This code is contributed by Ajit.
?>



Output:



2 4 7 10 11 14

Time Complexity : O(m + n).

This article is contributed by Dharmendra kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :