Open In App

Symmetric difference of two sorted array

Last Updated : 19 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 finding the symmetric difference of two sorted arrays is similar to merge process of merge sort. We traverse both arrays simultaneously and print smaller elements 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. 

Implementation:

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++;
        }
    }
    while (i < n) {
        cout << arr1[i] << " ";
        i++;
    }
    while (j < m) {
        cout << arr2[j] << " ";
        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++;
            }
        }
        while (i < n) {
            System.out.print(arr1[i] + " ");
            i++;
        }
        while (j < m) {
            System.out.print(arr2[j] + " ");
            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
 
    while i < n:
        print(arr1[i], end=' ')
        i += 1
 
    while j < m:
        print(arr2[j], end=' ')
        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++;
            }
        }
        while (i < n) {
            Console.Write(arr1[i] + " ");
            i++;
        }
 
        while (j < m) {
            Console.Write(arr2[j] + " ");
            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++;
        }
    }
   
    while ($i < $n) {
        echo($arr1[$i] . " ");
        $i++;
    }
 
    while ($j < $m) {
        echo($arr2[$j] . " ");
        $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.
?>


Javascript




<script>
 
    // Javascript program to find the symmetric
    // difference of two sorted array.
     
    function symmDiff(arr1, arr2, n, m)
    {
  
        // Traverse both arrays simultaneously.
        let 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]) {
                document.write(arr1[i] + " ");
                i++;
            }
            else if (arr2[j] < arr1[i]) {
                document.write(arr2[j] + " ");
                j++;
            }
  
            // If both elements same, move ahead
            // in both arrays.
            else {
                i++;
                j++;
            }
        }
        while (i < n) {
            document.write(arr1[i] + " ");
            i++;
        }
  
        while (j < m) {
            document.write(arr2[j] + " ");
            j++;
        }
    }
     
    let arr1 = [ 2, 4, 5, 7, 8, 10, 12, 15 ];
    let arr2 = [ 5, 8, 11, 12, 14, 15 ];
    let n = arr1.length;
    let m = arr2.length;
 
    symmDiff(arr1, arr2, n, m);
     
</script>


Output

2 4 7 10 11 14 

Time Complexity: O(m + n).



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads