Recall & Review
beginner
What is a HashSet in C#?
A HashSet is a collection that stores unique elements only. It does not allow duplicates and provides fast lookup, add, and remove operations.
Click to reveal answer
beginner
How does a HashSet ensure elements are unique?
HashSet uses a hash function to quickly check if an element already exists before adding it. If the element is already present, it won't add it again.
Click to reveal answer
beginner
Which method is used to add an element to a HashSet in C#?
The
Add() method is used to add an element. It returns true if the element was added, or false if it was already present.Click to reveal answer
beginner
What happens if you try to add a duplicate element to a HashSet?
The HashSet ignores the duplicate and does not add it again. The
Add() method returns false to indicate the element was not added.Click to reveal answer
beginner
How can you check if a HashSet contains a specific element?
Use the
Contains() method. It returns true if the element is in the HashSet, otherwise false.Click to reveal answer
What does a HashSet in C# store?
✗ Incorrect
A HashSet stores only unique elements and does not allow duplicates.
Which method adds an element to a HashSet?
✗ Incorrect
The Add() method is used to add elements to a HashSet.
What does Add() return if you add a duplicate element to a HashSet?
✗ Incorrect
Add() returns false if the element is already in the HashSet and not added again.
How can you check if a HashSet contains an element?
✗ Incorrect
Contains() checks if an element is present in the HashSet.
Which of these is NOT a feature of HashSet?
✗ Incorrect
HashSet does NOT allow duplicates; it only stores unique elements.
Explain what a HashSet is and why it is useful for storing unique elements.
Think about how you keep a list of friends without repeats.
You got /3 concepts.
Describe how you would add elements to a HashSet and check if an element exists.
Adding is like putting a name on a guest list; checking is like asking if a name is already there.
You got /3 concepts.