0
0
C Sharp (C#)programming~10 mins

Set operations (Union, Intersect, Except) in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a union of two sets.

C Sharp (C#)
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));
Drag options to blanks, or click blank then click option'
AExcept
BUnionWith
CIntersect
DUnion
Attempts:
3 left
💡 Hint
Common Mistakes
Using UnionWith which modifies the original set instead of returning a new one.
Using Intersect which returns only common elements.
2fill in blank
medium

Complete the code to find the intersection of two sets.

C Sharp (C#)
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));
Drag options to blanks, or click blank then click option'
AExcept
BUnion
CIntersect
DUnionWith
Attempts:
3 left
💡 Hint
Common Mistakes
Using Except which returns elements only in the first set.
Using Union which returns all elements from both sets.
3fill in blank
hard

Fix the error in the code to get the difference of two sets.

C Sharp (C#)
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));
Drag options to blanks, or click blank then click option'
AIntersect
BExcept
CUnionWith
DUnion
Attempts:
3 left
💡 Hint
Common Mistakes
Using Intersect which returns common elements.
Using Union which returns all elements combined.
4fill in blank
hard

Fill both blanks to create a set of fruits that are in set1 but not in set2, then print the count.

C Sharp (C#)
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]);
Drag options to blanks, or click blank then click option'
AExcept
BCount
CLength
DIntersect
Attempts:
3 left
💡 Hint
Common Mistakes
Using Intersect instead of Except.
Using Length which is not a property of HashSet.
5fill in blank
hard

Fill the blanks to create a union of two sets, remove elements in the third set, and print the final elements.

C Sharp (C#)
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));
Drag options to blanks, or click blank then click option'
AUnion
BExcept
CIntersect
DUnionWith
Attempts:
3 left
💡 Hint
Common Mistakes
Using Intersect instead of Union.
Using UnionWith where a method returning a new set is needed.