Check if a HashSet contains the specified element in C#

In C#, you can check if a HashSet contains a specified element using the Contains() method. This method returns true if the element exists in the HashSet, and false otherwise. The Contains() method provides O(1) average time complexity, making it very efficient for membership testing.

Syntax

Following is the syntax for using the Contains() method −

bool result = hashSet.Contains(element);

Parameters

  • element − The element to locate in the HashSet.

Return Value

The Contains() method returns a bool value −

  • true if the element is found in the HashSet
  • false if the element is not found in the HashSet

Using Contains() with Integer HashSet

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      HashSet set1 = new HashSet();
      set1.Add(25);
      set1.Add(50);
      set1.Add(75);
      set1.Add(100);
      set1.Add(125);
      set1.Add(150);
      Console.WriteLine("Elements in HashSet1");
      foreach(int val in set1){
         Console.WriteLine(val);
      }
      
      HashSet set2 = new HashSet();
      set2.Add(30);
      set2.Add(60);
      set2.Add(100);
      set2.Add(150);
      set2.Add(200);
      set2.Add(250);
      Console.WriteLine("Elements in HashSet2");
      foreach(int val in set2){
         Console.WriteLine(val);
      }
      
      Console.WriteLine("Do they share common elements? "+set1.Overlaps(set2));
      Console.WriteLine("Does HashSet1 has element 60? "+set1.Contains(60));
      Console.WriteLine("Does HashSet2 has element 60? "+set2.Contains(60));
   }
}

The output of the above code is −

Elements in HashSet1
25
50
75
100
125
150
Elements in HashSet2
30
60
100
150
200
250
Do they share common elements? True
Does HashSet1 has element 60? False
Does HashSet2 has element 60? True

Using Contains() with String HashSet

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      HashSet hashSet = new HashSet();
      hashSet.Add("Tim");
      hashSet.Add("Jack");
      hashSet.Add("Matt");
      hashSet.Add("Steve");
      hashSet.Add("David");
      hashSet.Add("Kane");
      hashSet.Add("Gary");
      
      Console.WriteLine("Elements in HashSet");
      foreach(string val in hashSet){
         Console.WriteLine(val);
      }
      
      if (hashSet.Contains("Matt"))
         Console.WriteLine("The element Matt is in the HashSet");
      else
         Console.WriteLine("The element Matt is not in the HashSet");
         
      Console.WriteLine("Contains 'John': " + hashSet.Contains("John"));
      Console.WriteLine("Contains 'David': " + hashSet.Contains("David"));
   }
}

The output of the above code is −

Elements in HashSet
Tim
Jack
Matt
Steve
David
Kane
Gary
The element Matt is in the HashSet
Contains 'John': False
Contains 'David': True

Using Contains() for Conditional Operations

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      HashSet numbers = new HashSet {10, 20, 30, 40, 50};
      
      // Check multiple elements
      int[] testElements = {15, 30, 45, 50};
      
      foreach(int element in testElements) {
         if(numbers.Contains(element)) {
            Console.WriteLine($"{element} is present in the HashSet");
         } else {
            Console.WriteLine($"{element} is not present in the HashSet");
         }
      }
      
      // Conditional addition
      int newElement = 60;
      if(!numbers.Contains(newElement)) {
         numbers.Add(newElement);
         Console.WriteLine($"Added {newElement} to HashSet");
      }
      
      Console.WriteLine("Final HashSet contains: " + string.Join(", ", numbers));
   }
}

The output of the above code is −

15 is not present in the HashSet
30 is present in the HashSet
45 is not present in the HashSet
50 is present in the HashSet
Added 60 to HashSet
Final HashSet contains: 10, 20, 30, 40, 50, 60

Conclusion

The Contains() method in HashSet provides an efficient way to check element membership with O(1) average time complexity. It returns a boolean value indicating whether the specified element exists in the HashSet, making it ideal for fast lookups and conditional operations.

Updated on: 2026-03-17T07:04:36+05:30

891 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements