C# | Check if a HashSet contains the specified element
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
Syntax:
mySet.Contains(T item);
Here, mySet is the name of the HashSet and item is the required element to locate in the HashSet
Return Type: This method returns true if the HashSet
Below given are some examples to understand the implementation in a better way:
Example 1:
// 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"); } } |
Required Element is present
Example 2:
// 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"); } } |
Required Element is not present
Reference:
Recommended Posts:
- C# | Add element to HashSet
- C# | Remove the specified element from a HashSet
- C# | Check if two HashSet<T> objects are equal
- C# | Check if HashSet and the specified collection contain the same elements
- C# | Check if a HashSet is a subset of the specified collection
- C# | Check if a HashSet is a superset of the specified collection
- C# | Check if a HashSet is a proper superset of the specified collection
- C# | Check if a HashSet is a proper subset of the specified collection
- C# | Check if a HashSet and a specified collection share common elements
- C# | Check if an element is in the Collection<T>
- C# | How to check whether a List contains a specified element
- C# | Check if a Stack contains an element
- C# | Check if an element is in the Queue
- C# | Check whether an element is contained in the ArrayList
- C# | Check if the SortedSet contains a specific element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



