Junior — Middle
Can you explain what a HashSet data structure is and what it is used for?
sobes.tech AI
Answer from AI
HashSet is a collection in C# that stores unique elements without a specific order. It is based on a hash table, which provides very fast checks for the presence of an element, as well as addition and removal—usually in constant time.
The main purpose of HashSet is to store a set of unique values and efficiently perform set operations such as union, intersection, and difference.
Example usage:
var set = new HashSet<int>();
set.Add(1);
set.Add(2);
set.Add(2); // will not be added, as 2 already exists
if (set.Contains(1))
{
Console.WriteLine("Element 1 is in the set");
}