C# | Check if a HashSet contains the specified element Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report A HashSet is an unordered collection of the unique elements. It is found in 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. HashSet.Contains(T) Method is used to check whether a HashSet object contains the specified element. Syntax: mySet.Contains(T item); Here, mySet is the name of the HashSet and item is the required element to locate in the HashSet object. Return Type: This method returns true if the HashSet object contains the specified element; otherwise, false. Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to check if a HashSet // contains the specified element using 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 Example 2: CSHARP // C# code to check if a HashSet // contains the specified element using 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); } // Check if a HashSet contains // the specified element if (mySet.Contains(5)) Console.WriteLine("Required Element is present"); else Console.WriteLine("Required Element is not present"); } } Output: Required Element is not present Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.contains?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Check if a HashSet contains the specified element S Sahil_Bansall Follow Improve Article Tags : Misc C# CSharp-method CSharp-Generic-HashSet CSharp-Generic-Namespace +1 More Practice Tags : Misc Similar Reads C# | Check if the SortedSet contains a specific element SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.Contains(T) Method is used to check if a SortedSet contains a specific element or not. Properties: In C#, SortedSet class can be used to store, remov 2 min read C# | Check if HashSet and the specified collection contain the same elements Here's an example code that demonstrates how to use the Overlaps method to check if a HashSet and a specified collection share common elements in C#: C# using System; using System.Collections.Generic; class Program { static void Main(string[] args) { // Create a HashSet and a List HashSet<int> 3 min read C# | Check if the Hashtable contains a specific Key The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.ContainsKey(Object) Method is used to check whether the Hashtable contains a specific key or not. Syntax: public v 2 min read C# | Check if the Hashtable contains a specific Value The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.ContainsValue(Object) Method is used to check whether the Hashtable contains a specific value or not. Syntax: publ 2 min read C# | Check if a HashSet and a specified collection share common elements In C#, you can use the SetEquals method to check if a HashSet and a specified collection contain the same elements. The SetEquals method returns true if the HashSet and the specified collection contain the same elements; otherwise, it returns false. Here is an example code that demonstrates how to u 3 min read C# | Check if a HashSet is a subset of the specified collection A HashSet is an unordered collection of the unique elements. It is found in 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. HashSet.I 2 min read C# | Check if a Stack contains an element Stack represents a last-in, first out collection of object. Stack<T>.Contains(Object) Method is used to check whether an element is in the Stack<T> or not. Syntax: public virtual bool Contains(object obj); Return Value: The function returns True if the element exists in the Stack<T 2 min read C# | Check if a HashSet is a superset of the specified collection Sure, here's an example code that demonstrates using the IsSupersetOf method to check if a HashSet<string> is a superset of a List<string> and then printing out a message indicating the result: C# using System; using System.Collections.Generic; public class Program { public static void M 3 min read C# | Remove the specified element from a HashSet A HashSet 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. HashSet 3 min read C# | Check if SortedSet and the specified collection contain the same elements SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet.SetEquals(IEnumerable) Method is used to check whether the SortedSet and the specified collection contain the same elements. Properties: In C#, SortedSet c 2 min read Like