In this post we will be discussing about HashSet in C#. HashSet holds a set of objects which allows you to easily and quickly determine whether an object is already added or not. In simple terms HashSet represents a set of values. It is an un-ordered collection containing only unique elements.
One situation where HashSet will be useful is in getting the distinct values from array or list where duplicates may exist. HashSet contains only unique elements, once the element is added HashSet is quick to determine that the element is present or not. Lets take an example :
class Program
{
static void Main(string[] args)
{
int[] array = { 1, 2, 3, 4, 3, 5, 2 };
Console.WriteLine(string.Join(", ", array));
var hash = new HashSet<int>(array);
int[] array1 = hash.ToArray();
Console.WriteLine(string.Join(", ", array1));
}
}
In the above example we have created an integer array with duplicate elements. Then we have created an HashSet with the same array. The output of the above code is shown below. As you can see in the output that without HashSet it displays all the elements whereas with HashSet it is displaying only unique elements.
If you can use is right HashSet<T> is a beautiful collection, you can think of it as a simplified Dictionary<T,T>. That is, a Dictionary where TKey and TValue refer to the same object. So whenever you need a Dictionary<T,T> where you want to store the same value for TKey and TValue use HashSet<T>. It uses a hashing algorithm to find the items in the set, which means it does take up some additional space, but it is very fast.
The best thing about HashSet is that the time is constant for adds, removes or contains whether the collection is small or large. It roughly takes the same amount of time to find an item or determine if it's not in the collection.
There are lot of operations which you can do over HashSet like Union, Intersection, Overlap etc : Example :
class Program
{
static void Main(string[] args)
{
var hash1 = new HashSet<int> { 1, 2, 3 };
var hash2 = new HashSet<int> { 3, 4, 5 };
var set1 = hash1;
var set2 = hash2;
set1.IntersectWith(set2);
Console.WriteLine("Intersect : " + string.Join(", " , set1));
set1 = hash1;
set2 = hash2;
set1.UnionWith(set2);
Console.WriteLine("Union : " + string.Join(", ", set1));
}
}
Please Like and Share the Blog, if you find it interesting and helpful.
One situation where HashSet will be useful is in getting the distinct values from array or list where duplicates may exist. HashSet contains only unique elements, once the element is added HashSet is quick to determine that the element is present or not. Lets take an example :
class Program
{
static void Main(string[] args)
{
int[] array = { 1, 2, 3, 4, 3, 5, 2 };
Console.WriteLine(string.Join(", ", array));
var hash = new HashSet<int>(array);
int[] array1 = hash.ToArray();
Console.WriteLine(string.Join(", ", array1));
}
}
In the above example we have created an integer array with duplicate elements. Then we have created an HashSet with the same array. The output of the above code is shown below. As you can see in the output that without HashSet it displays all the elements whereas with HashSet it is displaying only unique elements.
If you can use is right HashSet<T> is a beautiful collection, you can think of it as a simplified Dictionary<T,T>. That is, a Dictionary where TKey and TValue refer to the same object. So whenever you need a Dictionary<T,T> where you want to store the same value for TKey and TValue use HashSet<T>. It uses a hashing algorithm to find the items in the set, which means it does take up some additional space, but it is very fast.
The best thing about HashSet is that the time is constant for adds, removes or contains whether the collection is small or large. It roughly takes the same amount of time to find an item or determine if it's not in the collection.
There are lot of operations which you can do over HashSet like Union, Intersection, Overlap etc : Example :
class Program
{
static void Main(string[] args)
{
var hash1 = new HashSet<int> { 1, 2, 3 };
var hash2 = new HashSet<int> { 3, 4, 5 };
var set1 = hash1;
var set2 = hash2;
set1.IntersectWith(set2);
Console.WriteLine("Intersect : " + string.Join(", " , set1));
set1 = hash1;
set2 = hash2;
set1.UnionWith(set2);
Console.WriteLine("Union : " + string.Join(", ", set1));
}
}
Please Like and Share the Blog, if you find it interesting and helpful.
No comments:
Post a Comment