Number of elements in HashSet in C#?

To get the number of elements in a HashSet in C#, use the Count property. The Count property returns an integer representing the total number of unique elements stored in the HashSet.

Syntax

Following is the syntax for getting the count of elements in a HashSet −

HashSet<T> hashSet = new HashSet<T>();
int count = hashSet.Count;

Using Count Property with Integer HashSet

The following example demonstrates how to get the number of elements in HashSets containing integers −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      HashSet<int> set1 = new HashSet<int>();
      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);
      }
      Console.WriteLine("Number of elements in HashSet1 = " + set1.Count);
      
      HashSet<int> set2 = new HashSet<int>();
      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("Number of elements in HashSet2 = " + set2.Count);
      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
Number of elements in HashSet1 = 6
Elements in HashSet2
30
60
100
150
200
250
Number of elements in HashSet2 = 6
Do they share common elements? True
Does HashSet1 has element 60? False
Does HashSet2 has element 60? True

Using Count Property with String HashSet

This example shows how HashSet automatically handles duplicate values and how the Count property reflects only unique elements −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      HashSet<string> set1 = new HashSet<string>();
      set1.Add("AB");
      set1.Add("CD");
      set1.Add("EF");
      set1.Add("AB");  // Duplicate - will not be added
      set1.Add("IJ");
      set1.Add("KL");
      set1.Add("EF");  // Duplicate - will not be added
      set1.Add("OP");
      Console.WriteLine("Elements in HashSet1");
      foreach(string val in set1) {
         Console.WriteLine(val);
      }
      Console.WriteLine("Number of elements in HashSet1 = " + set1.Count);
      
      HashSet<string> set2 = new HashSet<string>();
      set2.Add("EF");
      set2.Add("KL");
      Console.WriteLine("Number of elements in HashSet2 = " + set2.Count);
      Console.WriteLine("Elements in HashSet2");
      foreach(string val in set2) {
         Console.WriteLine(val);
      }
      Console.WriteLine("Is set1 a superset of set2? " + set1.IsSupersetOf(set2));
   }
}

The output of the above code is −

Elements in HashSet1
AB
CD
EF
IJ
KL
OP
Number of elements in HashSet1 = 6
Number of elements in HashSet2 = 2
Elements in HashSet2
EF
KL
Is set1 a superset of set2? True

Key Points

  • The Count property returns the number of unique elements in the HashSet.

  • Adding duplicate elements does not increase the count, as HashSet automatically prevents duplicates.

  • The Count property has O(1) time complexity, making it very efficient for getting the size.

  • An empty HashSet returns a count of 0.

Conclusion

The Count property provides an efficient way to determine the number of unique elements in a HashSet in C#. Since HashSet automatically handles duplicates, the count always reflects the actual number of distinct elements stored in the collection.

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

279 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements