C# HashSet

C# HashSet<T> is a collection class in the .NET framework that represents a set of unique elements. It is part of the System.Collections.Generic namespace. HashSet<T> provides fast lookup and retrieval operations for adding, removing, and testing for the presence of elements based on their values.

Here are some key points about HashSet<T>:

  1. Unique Elements: HashSet<T> ensures that all elements in the collection are unique. If you attempt to add duplicate elements, they will be ignored.
  2. Performance: HashSet<T> provides efficient performance for operations such as adding, removing, and checking for the presence of elements. These operations have an average time complexity of O(1) (constant time).
  3. Unordered Collection: HashSet<T> does not maintain any particular order of elements. If you need to preserve the order, you should consider using a different collection type, such as List<T> or SortedSet<T>.
  4. Generic Type: The “T” in HashSet<T> represents the type of elements that the set can contain. It can be any valid C# data type or a custom class.
  5. Hashing: HashSet<T> internally uses hashing techniques to efficiently organize and locate elements. This allows for fast lookup and retrieval operations.
  6. Equality Comparison: HashSet<T> uses the default equality comparer for the element type T to determine whether two elements are equal or not. If you need custom equality logic, you can implement the IEqualityComparer<T> interface and provide a custom comparer when creating the HashSet<T>.
  7. Common Operations: HashSet<T> provides various methods for adding elements (Add), removing elements (Remove), checking for the presence of an element (Contains), clearing the set (Clear), and more. It also supports set operations such as Union, Intersect, Except, and SymmetricExcept.

Here’s an example of how to use HashSet<T>:

HashSet<string> set = new HashSet<string>();

Console.WriteLine(set.Contains("apple"));  // true
Console.WriteLine(set.Contains("grape"));  // false

Console.WriteLine(set.Count);  // 2

Console.WriteLine(set.Count);  // 0

In the example above, a HashSet<string> is created, and elements are added using the Add method. The Contains method is used to check if an element exists in the set. The Remove method removes an element, and the Count property gives the number of elements in the set. Finally, the Clear method removes all elements from the set.

HashSet<T> is a versatile collection class that is commonly used when you need to store a unique set of elements and perform efficient lookup operations based on their values.

C# HashSet<T> example:

Certainly! Here’s an example that demonstrates the usage of HashSet<T> in C#:

using System;
using System.Collections.Generic;

class Program
    static void Main()
        // Create a HashSet of integers
        HashSet<int> numbers = new HashSet<int>();

        // Add elements to the HashSet

        // Check if an element exists in the HashSet
        Console.WriteLine(numbers.Contains(10));  // Output: True
        Console.WriteLine(numbers.Contains(20));  // Output: False

        // Add a duplicate element (will be ignored)

        // Remove an element from the HashSet

        // Print the elements in the HashSet
        foreach (int number in numbers)

In this example, a HashSet<int> named numbers is created. Elements (integers) are added to the set using the Add method. The Contains method is used to check if a specific element exists in the set. The Remove method removes an element from the set. Finally, the foreach loop is used to iterate over the elements in the HashSet and print them to the console.



Note that the order of the elements in the HashSet may vary, as HashSet does not guarantee a specific order. Additionally, duplicate elements are not allowed in the HashSet, so adding a duplicate element will have no effect on the set.