How to use HashSet in C#
What is HashSet in C#? C# has a list class to which duplicate items cannot be added. This class is called "HashSet" and is very useful in some cases. Since it can be used in the same way as the regular List class, it is well worth mastering.
Learn more:
Features of the HashSet class
- Duplicate objects cannot be added.
- Duplicate checking is not required since all values must be unique.
How to use the HashSet class
To use the HashSet class, you must declare the following namespace:
using System.Collections.Generic;
As with List, assign the instantiated class to a variable.
Example:
HashSet<string> list = new HashSet<string>();
Or:
var list = new Hashset<string>();
Set elements into a HashSet
To set elements into a HashSet, you can write as follows:
HashSet<string> fruits = new HashSet<string> { "Apple", "Melon", "Grape" };
The HashSet class is instantiated, and initial values are set.
Adding an element using the Add() method
You can add an element to a HashSet using the Add() method like this:
HashSet<string> fruits = new HashSet<string>(); fruits.Add("Grape");
Sample program:
using System; using System.Collections.Generic; namespace CSharpExample { class Program { static void Main(string[] args) { //Initialize HashSet<string> fruits = new HashSet<string> { "Apple", "Melon", "Grape" }; Print(fruits); Console.WriteLine("*********************"); //Add elements fruits.Add("Kiwi"); fruits.Add("Orange"); Print(fruits); } static void Print(HashSet<string> hashset) { foreach (string element in hashset) { Console.WriteLine("Element: {0}", element); } } } }
Output:
Element: Apple
Element: Melon
Element: Grape
*********************
Element: Apple
Element: Melon
Element: Grape
Element: Kiwi
Element: Orange
Cannot set an element if the same element already exists
Example:
HashSet<string> fruits = new HashSet<string>() { "Apple", "Melon", "Grape" }; //Add elements fruits.Add("Kiwi"); //Apple already exists fruits.Add("Apple");
Sample program:
using System; using System.Collections.Generic; namespace CSharpExample { class Program { static void Main(string[] args) { //Initialize HashSet<string> fruits = new HashSet<string> { "Apple", "Melon", "Grape" }; Print(fruits); Console.WriteLine("*********************"); //Add elements fruits.Add("Apple"); Print(fruits); } static void Print(HashSet<string> hashset) { foreach (string element in hashset) { Console.WriteLine("Element: {0}", element); } } } }
Output:
Element: Apple
Element: Melon
Element: Grape
*********************
Element: Apple
Element: Melon
Element: Grape
Check for the presence of the specified element
To check for the presence of the specified element, use the Contains() method as the following example shows:
HashSet<string> fruits = new HashSet<string>(); //Add elements fruits.Add("Kiwi"); fruits.Add("Grape"); fruits.Add("Mango"); fruits.Add("Apple"); //If "Apple" is present if (fruits.Contains("Apple")) { //Code }
Sample program:
using System; using System.Collections.Generic; namespace CSharpExample { class Program { static void Main(string[] args) { //Initialize HashSet<string> fruits = new HashSet<string> { "Apple", "Melon", "Grape" }; //Add elements fruits.Add("Orange"); Print(fruits); Console.WriteLine("*********************"); //Check if exists if (fruits.Contains("Orange")) { Console.WriteLine("\"Orange\" exists!"); } else { Console.WriteLine("\"Orange\" does not exist!"); } // if (fruits.Contains("Mango")) { Console.WriteLine("\"Mango\" exists!"); } else { Console.WriteLine("\"Mango\" does not exist!"); } } static void Print(HashSet<string> hashset) { foreach (string element in hashset) { Console.WriteLine("Element: {0}", element); } } } }
Output:
Element: Apple
Element: Melon
Element: Grape
Element: Orange
*********************
"Orange" exists!
"Mango" does not exist!
Deleting the specified element using the Remove method
Deletes an element by specifying the element. Here is an example:
HashSet<string> fruits = new HashSet<string>(); //Add elements fruits.Add("Grape"); fruits.Add("Apple"); fruits.Add("Mango"); //Remove fruits.Remove("Apple");
Get the number of elements using the Count property
Get the number of elements using the Count property. Here is an example:
HashSet<string> fruits = new HashSet<string>(); //Add elements fruits.Add("Grape"); fruits.Add("Apple"); fruits.Add("Mango"); fruits.Add("Kiwi"); //Get the number of elements int count = fruits.Count; //Remove fruits.Remove("Apple"); //Get the number of elements count = fruits.Count;
Sample program:
using System; using System.Collections.Generic; namespace CSharpExample { class Program { static void Main(string[] args) { //Initialize HashSet<string> fruits = new HashSet<string> { "Apple", "Melon", "Grape" }; //Add elements fruits.Add("Pineapple"); Print(fruits); Console.WriteLine("*********************"); Console.WriteLine("The number of elements: {0}", fruits.Count); } static void Print(HashSet<string> hashset) { foreach (string element in hashset) { Console.WriteLine("Element: {0}", element); } } } }
Output:
Element: Apple
Element: Melon
Element: Grape
Element: Pineapple
*********************
The number of elements: 4
Delete all elements using the Clear() method
To delete all elements from a HashSet, use the Clear() method. Here is an example:
HashSet<string> fruits = new HashSet<string>(); //Add elements fruits.Add("Kiwi"); fruits.Add("Grape"); fruits.Add("Mango"); fruits.Add("Apple"); //Delete all elements fruits.Clear();
Summary
In this tutorial, you have learned how to use the C# HashSet class. If you need a list with unique elements, use HashSet.