Set operations (Union, Intersect, Except) in C Sharp (C#) - Time & Space Complexity
When we use set operations like Union, Intersect, and Except, we want to know how long these actions take as the sets get bigger.
We ask: How does the time needed grow when the number of items in the sets grows?
Analyze the time complexity of the following code snippet.
var setA = new HashSet<int>() {1, 2, 3, 4, 5};
var setB = new HashSet<int>() {4, 5, 6, 7, 8};
var union = setA.Union(setB).ToHashSet();
var intersect = setA.Intersect(setB).ToHashSet();
var except = setA.Except(setB).ToHashSet();
This code creates two sets and finds their union, intersection, and difference.
Look at what repeats when doing these set operations.
- Primary operation: Checking each item in one set against the other set.
- How many times: Each item in the first set is checked once, and sometimes items in the second set are also checked.
As the sets get bigger, the number of checks grows roughly in a straight line with the total number of items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 checks |
| 100 | About 200 checks |
| 1000 | About 2000 checks |
Pattern observation: Doubling the size of the sets roughly doubles the work done.
Time Complexity: O(n + m)
This means the time grows linearly with the total number of items in both sets.
[X] Wrong: "Set operations always take the same time no matter how big the sets are."
[OK] Correct: Actually, the time depends on how many items are in the sets because each item needs to be checked.
Understanding how set operations scale helps you explain your code clearly and shows you know how to handle data efficiently.
"What if we used lists instead of sets for these operations? How would the time complexity change?"
Practice
Which set operation in C# returns all unique elements from two collections combined?
Solution
Step 1: Understand the meaning of Union
Union combines all unique elements from both collections without duplicates.Step 2: Compare with other operations
Intersect returns only common elements, Except returns elements only in the first collection, and Concat just joins collections without removing duplicates.Final Answer:
Union -> Option AQuick Check:
Union = all unique elements combined [OK]
- Confusing Union with Intersect
- Thinking Concat removes duplicates
- Mixing Except with Union
Which of the following is the correct syntax to get the intersection of two sets setA and setB in C#?
var result = ???;
Solution
Step 1: Identify method for intersection
The Intersect method returns elements common to both collections.Step 2: Verify syntax correctness
setA.Intersect(setB) is the correct syntax; Union and Except do different operations, and Join is unrelated.Final Answer:
setA.Intersect(setB) -> Option BQuick Check:
Intersect = common elements [OK]
- Using Union instead of Intersect
- Trying to use Join for sets
- Incorrect method call syntax
What is the output of the following C# code?
var setA = new HashSet<int>() {1, 2, 3, 4};
var setB = new HashSet<int>() {3, 4, 5, 6};
var result = setA.Except(setB);
Console.WriteLine(string.Join(",", result));Solution
Step 1: Understand Except operation
Except returns elements in the first set that are not in the second set.Step 2: Apply Except to given sets
setA has 1,2,3,4; setB has 3,4,5,6; elements in setA but not in setB are 1 and 2.Final Answer:
1,2 -> Option DQuick Check:
Except = elements only in first set [OK]
- Confusing Except with Intersect
- Including elements from second set
- Expecting all unique elements combined
Find the error in this C# code snippet that tries to get the union of two sets set1 and set2:
var set1 = new HashSet<string>() {"apple", "banana"};
var set2 = new HashSet<string>() {"banana", "cherry"};
var result = set1.Union(set2).ToList();
Console.WriteLine(result);Solution
Step 1: Check method usage
Union exists and works on HashSet; ToList() is valid after Union.Step 2: Identify Console.WriteLine issue
Console.WriteLine cannot print a List directly; it prints the type name instead.Final Answer:
Console.WriteLine cannot print a List directly -> Option CQuick Check:
Printing List needs string.Join or loop [OK]
- Assuming Union is invalid for HashSet
- Calling ToList before Union
- Thinking HashSet can't hold strings
You have two lists of integers representing IDs: list1 = {1, 2, 3, 4, 5} and list2 = {4, 5, 6, 7}. You want to find IDs that are either in list1 or list2 but not in both (exclusive or). Which C# code snippet correctly achieves this using set operations?
Solution
Step 1: Understand exclusive or (XOR) logic
XOR means elements in one set or the other but not both.Step 2: Analyze each option
var result = list1.Union(list2).Except(list1.Except(list2)); subtracts elements unique to list1 from the union, leaving common and list2-unique elements - incorrect. var result = list1.Except(list2).Union(list2.Except(list1)); combines elements unique to each list using Except and Union, which directly matches XOR.Final Answer:
var result = list1.Except(list2).Union(list2.Except(list1)); -> Option AQuick Check:
XOR = unique elements from each set combined [OK]
- Using Intersect instead of Except for uniqueness
- Confusing union and intersection order
- Trying to intersect except results
Ready to prove it?
Pass the test and this pattern moves unknown
