Write a java program to find Symmetric Difference

Write a program to read two integer array and calculate the symmetric difference of the two arrays. Finally, Sort the array.
Symmetric difference is the difference of A Union B and A Intersection B ie. [ (A U B) – (A ^ B)]
Union operation merges the two arrays and makes sure that common elements appear only once. Intersection operation includes common elements from both the arrays.
Ex – A={12,24,7,36,14} and B={11,26,7,14}.
A U B ={ 7,11,12,14,24,26,36} and
A ^ B = {7,14}
Symmetric difference of A and B after sorting= [A U B] – [ A ^ B] = {11,12,24,26,36}.

Input and Output Format:
Input consists of an integer n which is the number of elements followed by n integer values. The same sequnce is followed for the next array.
Output consists of sorted symmetric difference array.
Refer sample output for formatting specifications.

Sample Input 1:
5
11
5
14
26
3
3
5
3
1
Sample Output 1:
1
11
14
26

Symmetric Difference in Java.

import java.util.*;

public class Main {
  public static void main(String[] args) {
    int n, m;
    Scanner sin = new Scanner(System.in);
    n = sin.nextInt();
    int[] a1 = new int[n];
    for (int i = 0; i < n; i++) {
      a1[i] = sin.nextInt();
    }
    m = sin.nextInt();
    int[] a2 = new int[m];
    for (int i = 0; i < m; i++) {
      a2[i] = sin.nextInt();
    }
    int[] result = getSymmetricDifference(a1, a2);
    for (int i = 0; i < result.length; i++)
      System.out.println(result[i]);
  }

  public static int[] getSymmetricDifference(int[] a1, int[] a2) {

    // int[] a1 = new int[]{11,5,14,26,3};
    // int[] a2 = new int[]{5,3,1};
    int[] union, inter, result;

    int count = 0;
    int max = a1.length + a2.length;
    ArrayList<Integer> temp = new ArrayList<Integer>(max);

    /* union */

    for (int i = 0; i < a1.length; i++) {
      if (!temp.contains(a1[i])) {
        ++count;
        temp.add(a1[i]);
      }
    }
    for (int i = 0; i < a2.length; i++) {
      if (!temp.contains(a2[i])) {
        ++count;
        temp.add(a2[i]);
      }
    }
    union = new int[count];
    for (int i = 0; i < count; i++) {
      union[i] = (int) temp.get(i);
    }
    Arrays.sort(union);

    /* intersection */

    temp = new ArrayList<Integer>(max);
    count = 0;
    Arrays.sort(a2);
    for (int i = 0; i < a1.length; i++) {
      if (Arrays.binarySearch(a2, a1[i]) >= 0) {
        ++count;
        temp.add(a1[i]);
      }
    }
    inter = new int[count];
    for (int i = 0; i < count; i++) {
      inter[i] = (int) temp.get(i);
    }
    Arrays.sort(inter);

    /* difference */

    temp = new ArrayList<Integer>(max);
    count = 0;
    Arrays.sort(inter);
    for (int i = 0; i < union.length; i++) {
      if (Arrays.binarySearch(inter, union[i]) < 0) {
        ++count;
        temp.add(union[i]);
      }
    }
    result = new int[count];
    for (int i = 0; i < count; i++) {
      result[i] = (int) temp.get(i);
    }
    Arrays.sort(result);
    // System.out.println("resultant array : \n "+Arrays.toString(result));
    return result;

  }
}

 

Add a Comment

Your email address will not be published. Required fields are marked *

2 × five =