Question: Is my solution correct for this problem: Build a method calledsymmetricSetDifferencethat accepts twoSetsas parameters and returns a newSetcontaining their symmetric set difference (that is,
Is my solution correct for this problem:
"Build a method calledsymmetricSetDifferencethat accepts twoSetsas parameters and returns a newSetcontaining their symmetric set difference (that is, the set of elements contained in either of the two sets but not in both). For example, the symmetric difference between the sets[1, 4, 7, 9]and[2, 4, 5, 6, 7]is[1, 2, 5, 6, 9]."
Solution is:
public static SetsymmetricSetDifference(Set set1, Set set2) { Set temp = new HashSet<>(set1); temp.removeAll(set2); set2.removeAll(set1); Set result = new HashSet<>(temp); for(int num : set2) { result.add(num); } return result; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
