Problem Statement
A set of numbers is a collection of numbers with no repeated elements. We can define the following set operations:
The UNION of two sets A and B is a set containing all the elements that are either in A or in B.
The INTERSECTION of two sets A and B is a set containing all the elements that are in both A and B.
The SYMMETRIC DIFFERENCE of two sets A and B is a set containing all the elements that are either in A or in B, but not containing elements that are in both A and B.
Given two int[]s representing sets A and B, and an operation applied on them, return a int[] representing the resulting set sorted in ascending order. If the result is an empty set then return an empty int[]. operation will be one of the following: "UNION", "INTERSECTION", "SYMMETRIC DIFFERENCE".
Definition
- Class:
- Sets
- Method:
- operate
- Parameters:
- int[], int[], String
- Returns:
- int[]
- Method signature:
- int[] operate(int[] A, int[] B, String operation)
- (be sure your method is public)
Constraints
- A will have between 0 and 50 elements inclusive.
- B will have between 0 and 50 elements inclusive.
- each element in A will be between -1000000 and 1000000 inclusive.
- each element in B will be between -1000000 and 1000000 inclusive.
- A will not have any repeated elements.
- B will not have any repeated elements.
- operation will be one of the following: "UNION", "INTERSECTION", "SYMMETRIC DIFFERENCE".
Examples
{1,2,3,4}{3,4,5,6}"INTERSECTION"
Returns: { 3, 4 }The only elements that are both in A and in B are 3 and 4.{1,2,3,4}{3,4,5,6}"UNION"
Returns: { 1, 2, 3, 4, 5, 6 }Here we return all the elements that are either in A or in B.{432,756,123}{534,76,1209}"INTERSECTION"
Returns: { }There are no common elements, so we must return an empty set.{6,5,7,4}{7,6,4,10}"SYMMETRIC DIFFERENCE"
Returns: { 5, 10 }Elements 4, 6, 7 are in both sets, thus they cannot be in our answer. However we can include elements 5 and 10.{342,654,897,312,76,23,78}{21,43,87,98,23,756,897,234,645,876,123}"SYMMETRIC DIFFERENCE"
Returns: { 21, 43, 76, 78, 87, 98, 123, 234, 312, 342, 645, 654, 756, 876 }