Symmetric difference

Report a typo

The symmetric difference of two sets is the set that contains elements that are in either of the sets but not in their intersection. In other words, only those elements that are present in one set and not present in the other. Check out the illustration for better understanding:

Illustration of symmetric difference

Implement a method for finding the symmetric difference of the two given sets of strings. Elements in the resulting set can be in any order.

Example:

The symmetric difference of two sets {1, 2, 3} and {0, 1, 2} is {0, 3}

Sample Input 1:

1 2 3
0 1 2

Sample Output 1:

0 3
Write a program in Java 17
import java.util.*;

class SetUtils {

/**
* @return symmetric difference between set1 and set2
*/
public static Set<String> symmetricDifference(Set<String> set1, Set<String> set2) {
// write your code here
return Set.of();
}

}

/* Do not change the code below */
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

String strSet1 = scanner.nextLine();
String strSet2 = scanner.nextLine();

Set<String> set1 = new HashSet<>();
Set<String> set2 = new HashSet<>();

if (!Objects.equals(strSet1, "empty")) {
Collections.addAll(set1, strSet1.split("\\s+"));
}

if (!Objects.equals(strSet2, "empty")) {
Collections.addAll(set2, strSet2.split("\\s+"));
}

Set<String> resultSet = SetUtils.symmetricDifference(set1, set2);

System.out.println(String.join(" ", resultSet));
___

Create a free account to access the full topic