C# | Number of elements in HashSet Last Updated : 12 Jul, 2023 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. You can use HashSet.Count Property to count the number of elements in a HashSet. Syntax: mySet.Count; Here mySet is the HashSet Below given are some examples to understand the implementation in a better way: Example 1: CSHARP // C# code to get the number of // elements that are contained in HashSet 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); } // To get the number of // elements that are contained in HashSet Console.WriteLine(mySet.Count); } } Output5Example 2: CSHARP // C# code to get the number of // elements that are contained in HashSet 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>(); // To get the number of // elements that are contained in HashSet. // Note that, here the HashSet is empty Console.WriteLine(mySet.Count); } } Output0Using the AsEnumerable and Count methods AsEnumerable: The AsEnumerable method is used to convert a collection into an enumerable type, enabling the use of LINQ extension methods.Count: The Count method returns the number of elements in a collection, providing a convenient way to determine its size. C# using System; using System.Collections.Generic; using System.Linq; public class GFG { static public void Main() { // Create a HashSet HashSet<int> numbers = new HashSet<int>(); numbers.Add(1); numbers.Add(2); numbers.Add(3); // Get the count of elements using the Enumerable.Count() method int elementCount = numbers.AsEnumerable().Count(); // Display the result Console.WriteLine("Number of elements in the HashSet: " + elementCount); } } OutputNumber of elements in the HashSet: 3 In this code, we create a HashSet called numbers and add three integer values to it. Then, we use the AsEnumerable() method to convert the HashSet to an IEnumerable<int>, and the Count() method from the Enumerable class to get the count of elements in the HashSet. The main heading is printed, followed by the elements of the HashSet and the count of elements. Comment More infoAdvertise with us Next Article C# | Number of elements in HashSet S Sahil_Bansall Follow Improve Article Tags : Misc C# CSharp-Generic-HashSet CSharp-Generic-Namespace Practice Tags : Misc Similar Reads C# | Add element to 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. Elements 2 min read C# | Get the number of elements in the SortedSet SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.Count Property is used to get the number of elements in the SortedSet. Properties: In C#, SortedSet class can be used to store, remove or view elemen 2 min read Hashtable elements() Method in Java The elements() method in the Hashtable class in Java returns an enumeration of the values contained in the hashtable. The enumeration returned by this method traverses the hashtable's values in the order in which they were added to the hashtable. The elements() method is inherited from the java.util 2 min read C# | Remove all elements from a HashSet A HashSet is an unordered collection of the unique elements. It comes under the 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. HashS 2 min read C# | Number of elements contained in the BitArray The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e, 1 and false indicates the bit is off i.e, 0. This class is contained in System.Collections namespace. BitArray.Count property is used to get the number of element 2 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 Java HashSet contains() Method The contains() method of the HashSet class in Java is used to check if a specific element is present in the HashSet or not. So, mainly it is used to check if a set contains any particular element.Java// Java program to demonstrates the working of contains() import java.util.*; public class Geeks { p 1 min read C# | Count the total number of elements in the List List class represents the list of objects which can be accessed by index. It comes under the System.Collection.Generic namespace. List class can be used to create a collection of different types like integers, strings etc. List class also provides the methods to search, sort, and manipulate lists. L 2 min read C# | Remove all elements in a collection from a HashSet In C#, you can use the ExceptWith() method to remove all the elements in a collection from a HashSet. The ExceptWith() method removes all the elements in the specified collection from the current HashSet. Here is an example code that demonstrates how to use the ExceptWith() method to remove all the 3 min read Java HashSet clear() Method The clear() method of the HashSet class in Java is used to clear all the elements from the HahSet. This method makes the HashSet empty but does not delete the HashSet itself. It simply removes all elements, leaving the set ready for reuse.Syntax of HashSet clear() Methodvoid clear()Key Points:After 1 min read Like