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

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

Choose your learning style9 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.