How to resolve the algorithm Symmetric difference step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Symmetric difference step by step in the Java programming language

Table of Contents

Problem Statement

Given two sets A and B, compute

( A ∖ B ) ∪ ( B ∖ A ) .

{\displaystyle (A\setminus B)\cup (B\setminus A).}

That is, enumerate the items that are in A or B but not both. This set is called the symmetric difference of A and B. In other words:

( A ∪ B ) ∖ ( A ∩ B )

{\displaystyle (A\cup B)\setminus (A\cap B)}

(the set of items that are in at least one of A or B minus the set of items that are in both A and B). Optionally, give the individual differences (

A ∖ B

{\displaystyle A\setminus B}

and

B ∖ A

{\displaystyle B\setminus A}

) as well.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Symmetric difference step by step in the Java programming language

The provided Java code demonstrates how to find the symmetric difference between two sets. The symmetric difference of two sets is the set of elements that are in one set but not the other.

To find the symmetric difference, the code uses two approaches:

  1. Union of Differences:
  • The first approach calculates the symmetric difference by finding the differences between the two sets (elements that are in one set but not the other) and then taking the union of those differences.
    • To do this, it creates two new sets called notInSetA and notInSetB.
    • It populates notInSetA with all elements from setB that are not in setA and notInSetB with all elements from setA that are not in setB.
    • Finally, it creates a new set called symmetricDifference by combining notInSetA and notInSetB.
  1. Union Minus Intersection:
  • The second approach calculates the symmetric difference by first finding the union of the two sets (all elements in both sets) and then subtracting the intersection of the two sets (elements that are in both sets).
    • It creates a new set called union to store the union of setA and setB, and a new set called intersection to store the intersection of setA and setB.
    • It then creates a new set called symmetricDifference2 by subtracting intersection from union.

Both approaches produce the same result, which is the set of elements that are in one set but not the other (the symmetric difference).

The code also prints out the initial sets, the elements that are not in each set, and the symmetric differences calculated using both approaches.

Source code in the java programming language

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class SymmetricDifference {
    public static void main(String[] args) {
        Set<String> setA = new HashSet<String>(Arrays.asList("John", "Serena", "Bob", "Mary", "Serena"));
        Set<String> setB = new HashSet<String>(Arrays.asList("Jim", "Mary", "John", "Jim", "Bob"));

        // Present our initial data set
        System.out.println("In set A: " + setA);
        System.out.println("In set B: " + setB);

        // Option 1: union of differences
        // Get our individual differences.
        Set<String> notInSetA = new HashSet<String>(setB);
        notInSetA.removeAll(setA);
        Set<String> notInSetB = new HashSet<String>(setA);
        notInSetB.removeAll(setB);
 
        // The symmetric difference is the concatenation of the two individual differences
        Set<String> symmetricDifference = new HashSet<String>(notInSetA);
        symmetricDifference.addAll(notInSetB);
        
        // Option 2: union minus intersection
        // Combine both sets
        Set<String> union = new HashSet<String>(setA);
        union.addAll(setB);
        
        // Get the intersection
        Set<String> intersection = new HashSet<String>(setA);
        intersection.retainAll(setB);
        
        // The symmetric difference is the union of the 2 sets minus the intersection
        Set<String> symmetricDifference2 = new HashSet<String>(union);
        symmetricDifference2.removeAll(intersection);
 
        // Present our results
        System.out.println("Not in set A: " + notInSetA);
        System.out.println("Not in set B: " + notInSetB);
        System.out.println("Symmetric Difference: " + symmetricDifference);
        System.out.println("Symmetric Difference 2: " + symmetricDifference2);
    }
}


  

You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Reverse words in a string step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Delete a file step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Empty program step by step in the Pony programming language
You may also check:How to resolve the algorithm Pascal's triangle step by step in the F# programming language