Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Set operations (Union, Intersect, Except) in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Set Operations Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Union operation on two sets
What is the output of the following C# code that uses Union on two integer sets?
C Sharp (C#)
using System;
using System.Collections.Generic;
using System.Linq;

class Program {
    static void Main() {
        var setA = new HashSet<int> {1, 2, 3};
        var setB = new HashSet<int> {3, 4, 5};
        var unionSet = setA.Union(setB);
        foreach(var item in unionSet) {
            Console.Write(item + " ");
        }
    }
}
A1 2 3 4 5
B3 4 5
C1 2 3
D1 2 4 5
Attempts:
2 left
💡 Hint
Union combines all unique elements from both sets.
❓ Predict Output
intermediate
2:00remaining
Output of Intersect operation on two sets
What does the following C# code print when using Intersect on two sets?
C Sharp (C#)
using System;
using System.Collections.Generic;
using System.Linq;

class Program {
    static void Main() {
        var setA = new HashSet<int> {10, 20, 30, 40};
        var setB = new HashSet<int> {30, 40, 50, 60};
        var intersectSet = setA.Intersect(setB);
        foreach(var item in intersectSet) {
            Console.Write(item + " ");
        }
    }
}
A50 60
B10 20 30 40 50 60
C30 40
D10 20
Attempts:
2 left
💡 Hint
Intersect returns only elements present in both sets.
❓ Predict Output
advanced
2:00remaining
Output of Except operation on two sets
What is the output of this C# code using Except to find elements in setA not in setB?
C Sharp (C#)
using System;
using System.Collections.Generic;
using System.Linq;

class Program {
    static void Main() {
        var setA = new HashSet<string> {"apple", "banana", "cherry"};
        var setB = new HashSet<string> {"banana", "date"};
        var exceptSet = setA.Except(setB);
        foreach(var item in exceptSet) {
            Console.Write(item + " ");
        }
    }
}
Adate
Bapple cherry
Capple banana cherry date
Dbanana date
Attempts:
2 left
💡 Hint
Except returns elements in the first set that are not in the second.
🔧 Debug
advanced
2:00remaining
Identify the error in set operation code
This C# code tries to get the union of two sets but causes a compile error. What is the error?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        var setA = new HashSet<int> {1, 2, 3};
        var setB = new HashSet<int> {4, 5, 6};
        var unionSet = setA.Union(setB);
        foreach(int item in unionSet) {
            Console.WriteLine(item);
        }
    }
}
AMissing using System.Linq directive causes Union() to be undefined.
BHashSet<int> cannot use Union method; use AddRange instead.
CUnion method requires a List<int> argument, not HashSet<int>.
DCannot iterate unionSet because it is not enumerable.
Attempts:
2 left
💡 Hint
Check if all required namespaces are included for LINQ methods.
🧠 Conceptual
expert
2:00remaining
Number of elements after combined set operations
Given these sets:

Set A = {1, 2, 3, 4, 5}
Set B = {4, 5, 6, 7}
Set C = {5, 7, 8, 9}

What is the number of elements in the set resulting from:
var result = A.Union(B).Intersect(C);
A4
B3
C5
D2
Attempts:
2 left
💡 Hint
First find A union B, then find intersection with C.

Practice

(1/5)
1.

Which set operation in C# returns all unique elements from two collections combined?

easy
A. Union
B. Intersect
C. Except
D. Concat

Solution

  1. Step 1: Understand the meaning of Union

    Union combines all unique elements from both collections without duplicates.
  2. 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.
  3. Final Answer:

    Union -> Option A
  4. Quick Check:

    Union = all unique elements combined [OK]
Hint: Union = combine all unique items from both sets [OK]
Common Mistakes:
  • Confusing Union with Intersect
  • Thinking Concat removes duplicates
  • Mixing Except with Union
2.

Which of the following is the correct syntax to get the intersection of two sets setA and setB in C#?

var result = ???;
easy
A. setA.Join(setB)
B. setA.Intersect(setB)
C. setA.Except(setB)
D. setA.Union(setB)

Solution

  1. Step 1: Identify method for intersection

    The Intersect method returns elements common to both collections.
  2. Step 2: Verify syntax correctness

    setA.Intersect(setB) is the correct syntax; Union and Except do different operations, and Join is unrelated.
  3. Final Answer:

    setA.Intersect(setB) -> Option B
  4. Quick Check:

    Intersect = common elements [OK]
Hint: Use Intersect() to find common elements between sets [OK]
Common Mistakes:
  • Using Union instead of Intersect
  • Trying to use Join for sets
  • Incorrect method call syntax
3.

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));
medium
A. 1,2,5,6
B. 3,4
C. 5,6
D. 1,2

Solution

  1. Step 1: Understand Except operation

    Except returns elements in the first set that are not in the second set.
  2. 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.
  3. Final Answer:

    1,2 -> Option D
  4. Quick Check:

    Except = elements only in first set [OK]
Hint: Except returns items only in first set, not in second [OK]
Common Mistakes:
  • Confusing Except with Intersect
  • Including elements from second set
  • Expecting all unique elements combined
4.

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);
medium
A. Union method does not exist for HashSet
B. ToList() should be called before Union
C. Console.WriteLine cannot print a List directly
D. HashSet cannot contain strings

Solution

  1. Step 1: Check method usage

    Union exists and works on HashSet; ToList() is valid after Union.
  2. Step 2: Identify Console.WriteLine issue

    Console.WriteLine cannot print a List directly; it prints the type name instead.
  3. Final Answer:

    Console.WriteLine cannot print a List directly -> Option C
  4. Quick Check:

    Printing List needs string.Join or loop [OK]
Hint: Use string.Join to print collections, not Console.WriteLine directly [OK]
Common Mistakes:
  • Assuming Union is invalid for HashSet
  • Calling ToList before Union
  • Thinking HashSet can't hold strings
5.

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?

hard
A. var result = list1.Except(list2).Union(list2.Except(list1));
B. var result = list1.Intersect(list2).Except(list1.Union(list2));
C. var result = list1.Union(list2).Except(list1.Except(list2));
D. var result = list1.Except(list2).Intersect(list2.Except(list1));

Solution

  1. Step 1: Understand exclusive or (XOR) logic

    XOR means elements in one set or the other but not both.
  2. 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.
  3. Final Answer:

    var result = list1.Except(list2).Union(list2.Except(list1)); -> Option A
  4. Quick Check:

    XOR = unique elements from each set combined [OK]
Hint: XOR = (A except B) union (B except A) [OK]
Common Mistakes:
  • Using Intersect instead of Except for uniqueness
  • Confusing union and intersection order
  • Trying to intersect except results
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard