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

HashSet for unique elements in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AOnly unique elements
BElements in sorted order
CKey-value pairs
DDuplicates allowed
Which method adds an element to a HashSet?
AInsert()
BPush()
CAppend()
DAdd()
What does Add() return if you add a duplicate element to a HashSet?
Afalse
Bthrows an error
Cnull
Dtrue
How can you check if a HashSet contains an element?
AFind()
BExists()
CContains()
DSearch()
Which of these is NOT a feature of HashSet?
AStores unique elements
BAllows duplicates
CFast lookup
DNo guaranteed order
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.