Complete the code to create a union of two sets.
var set1 = new HashSet<int> {1, 2, 3};
var set2 = new HashSet<int> {3, 4, 5};
var unionSet = set1.[1](set2);
Console.WriteLine(string.Join(", ", unionSet));UnionWith which modifies the original set instead of returning a new one.Intersect which returns only common elements.The Union method returns a new set that contains all elements from both sets.
Complete the code to find the intersection of two sets.
var setA = new HashSet<string> {"apple", "banana", "cherry"};
var setB = new HashSet<string> {"banana", "date", "fig"};
var common = setA.[1](setB);
Console.WriteLine(string.Join(", ", common));Except which returns elements only in the first set.Union which returns all elements from both sets.The Intersect method returns elements that appear in both sets.
Fix the error in the code to get the difference of two sets.
var firstSet = new HashSet<int> {10, 20, 30, 40};
var secondSet = new HashSet<int> {30, 40, 50};
var difference = firstSet.[1](secondSet);
Console.WriteLine(string.Join(", ", difference));Intersect which returns common elements.Union which returns all elements combined.The Except method returns elements in the first set that are not in the second set.
Fill both blanks to create a set of fruits that are in set1 but not in set2, then print the count.
var set1 = new HashSet<string> {"apple", "banana", "cherry", "date"};
var set2 = new HashSet<string> {"banana", "date", "fig"};
var result = new HashSet<string>(set1.[1](set2));
Console.WriteLine(result.[2]);Intersect instead of Except.Length which is not a property of HashSet.Except returns elements in set1 not in set2. Count gives the number of elements in the result.
Fill the blanks to create a union of two sets, remove elements in the third set, and print the final elements.
var setA = new HashSet<int> {1, 2, 3};
var setB = new HashSet<int> {3, 4, 5};
var setC = new HashSet<int> {2, 5};
var union = setA.[1](setB);
var finalSet = union.[2](setC);
Console.WriteLine(string.Join(", ", finalSet));Intersect instead of Union.UnionWith where a method returning a new set is needed.Union combines setA and setB. Then Except removes elements in setC from the union.