C# | HashSet Class

20 Dec, 2018
A HashSet<T> is an unordered collection of the unique elements. It comes under System.Collections.Generic namespace. It is used in a situation where we want to prevent duplicates from being inserted in the collection. As far as performance is concerned, it is better in comparison to the list.
Characteristics of HashSet Class:
The HashSet<T> class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.
The capacity of a HashSet<T> object is the number of elements that the object can hold.
A HashSet<T> object’s capacity automatically increases as elements are added to the object.
A HashSet<T> collection is not sorted and cannot contain duplicate elements.
HashSet<T> provides many mathematical set operations, such as set addition (unions) and set subtraction.
Example:
// C# code to create a HashSetusing System;using System.Collections.Generic;  class GFG {      // Driver code    public static void Main()    {          // Creating a HashSet of odd numbers        HashSet<int> odd = new HashSet<int>();          // Inserting elements in HashSet        for (int i = 0; i < 5; i++) {            odd.Add(2 * i + 1);        }          // Displaying the elements in the HashSet        foreach(int i in odd)        {            Console.WriteLine(i);        }    }}
Output:
1
3
5
7
9

Example:
// C# code to get the number of// elements that are contained in HashSetusing System;using System.Collections.Generic;  class GFG {      // Driver code    public static void Main()    {          // Creating a HashSet of integers        HashSet<int> mySet = new HashSet<int>();          // Inserting elements in HashSet        for (int i = 0; i < 5; i++) {            mySet.Add(i * 2);        }          // To get the number of        // elements that are contained in HashSet        Console.WriteLine(mySet.Count);    }}
Output:
5

Example:
// C# code to Check if a HashSet is// a subset of the specified collectionusing System;using System.Collections.Generic;  class GFG {      // Driver code    public static void Main()    {          // Creating a HashSet of integers        HashSet<int> mySet1 = new HashSet<int>();          // Inserting elements in HashSet        // mySet1 only contains even numbers less than        // equal to 10        for (int i = 1; i <= 5; i++)            mySet1.Add(2 * i);          // Creating a HashSet of integers        HashSet<int> mySet2 = new HashSet<int>();          // Inserting elements in HashSet        // mySet2 contains all numbers from 1 to 10        for (int i = 1; i <= 10; i++)            mySet2.Add(i);          // Check if a HashSet mySet1 is a subset        // of the HashSet mySet2        Console.WriteLine(mySet1.IsSubsetOf(mySet2));    }}
Output:
True

Example:
// C# code to check if a HashSet// contains the specified elementusing System;using System.Collections.Generic;  class GFG {      // Driver code    public static void Main()    {          // Creating a HashSet of strings        HashSet<string> mySet = new HashSet<string>();          // Inserting elements in HashSet        mySet.Add("DS");        mySet.Add("C++");        mySet.Add("Java");        mySet.Add("JavaScript");          // Check if a HashSet contains        // the specified element        if (mySet.Contains("Java"))            Console.WriteLine("Required Element is present");        else            Console.WriteLine("Required Element is not present");    }}
Output:
Required Element is present

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1?view=netframework-4.7.2
CSharp-Generic-HashSet
CSharp-Generic-Namespace
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.