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

HashSet for unique elements in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
HashSet Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this HashSet code?

Consider the following C# code using HashSet<int>. What will be printed?

C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        HashSet<int> numbers = new HashSet<int>() {1, 2, 3, 2, 4, 1};
        Console.WriteLine(numbers.Count);
    }
}
A4
B5
C6
D3
Attempts:
2 left
💡 Hint

HashSet stores only unique elements. Duplicate values are ignored.

Predict Output
intermediate
2:00remaining
What does this code print after adding elements?

What will be the output of this C# program?

C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        HashSet<string> fruits = new HashSet<string>();
        fruits.Add("apple");
        fruits.Add("banana");
        fruits.Add("apple");
        fruits.Add("orange");
        Console.WriteLine(fruits.Count);
    }
}
A4
B3
C2
D1
Attempts:
2 left
💡 Hint

HashSet ignores duplicates when adding.

🔧 Debug
advanced
2:00remaining
Why does this HashSet code throw an exception?

Examine the code below. It throws a runtime exception. What is the cause?

C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        HashSet<int> set = new HashSet<int>() {1, 2, 3};
        foreach (var item in set) {
            if (item == 2) {
                set.Remove(item);
            }
        }
    }
}
AThe HashSet is empty, so Remove fails.
BHashSet does not support Remove method.
CThe foreach loop variable 'item' is read-only and cannot be used in Remove.
DModifying a collection while iterating causes an InvalidOperationException.
Attempts:
2 left
💡 Hint

Think about what happens if you change a collection while looping over it.

Predict Output
advanced
2:00remaining
What is the output of this HashSet intersection code?

What will this program print?

C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        HashSet<int> setA = new HashSet<int>() {1, 2, 3, 4};
        HashSet<int> setB = new HashSet<int>() {3, 4, 5, 6};
        setA.IntersectWith(setB);
        Console.WriteLine(string.Join(",", setA));
    }
}
A5,6
B1,2
C3,4
D1,2,3,4,5,6
Attempts:
2 left
💡 Hint

IntersectWith keeps only elements present in both sets.

🧠 Conceptual
expert
2:00remaining
What is the time complexity of HashSet operations?

Which statement best describes the average time complexity of Add, Remove, and Contains operations in a C# HashSet<T>?

AAdd, Remove, and Contains operations run on average in O(1) time due to hashing.
BAdd and Remove are O(1), but Contains is O(n) because it searches linearly.
CAll operations run in O(n) time because HashSet uses a list internally.
DAll operations run in O(log n) time because HashSet uses a balanced tree.
Attempts:
2 left
💡 Hint

Think about how hashing helps find elements quickly.