Removing duplicate values from Collections using HashSet
You might be faced issue of duplicate elements in the collections like Array, List etc. There are many ways to remove these duplicates from the collection and they have their own pros and cons. I have seen many times that people use loops to remove these duplicates but there are many options which remove the use of loop.
.Net framework 3.5 introduces a new class under System.Collections.Generic which removes duplicates from collections very efficiently. It provides high performance.
Name of this new class is HashSet. If we go to the documentation of this class on MDSN ( http://msdn.microsoft.com/en-us/library/bb359438(v=vs.90).aspx ) we have the below points to remember about this class:
- It represents a set of values
- It provides high performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.
- Its capacity is the number of elements that the object can hold and its capacity automatically increases as elements are added to the object.
- Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
- It implements ICollection
, IEnumerable , IEnumerable, ISerializable, IDeserializationCallback interfaces.
This class (HashSet) is available from .net framework 3.5 onwards (i.e. 3.5, 4.0, 4.5 versions).
Let’s see an example of this class. Suppose we have and array or string which contains 10 values and out of 10 values there are 4 duplicates. Now we have to remove these duplicates. See the code below
String[] arrString = new String[10]{“A”,”B”,”C”,”A”,”D”,”A”,”E”,”D”,”F”,”A”}
Now convert this string array to hash set which will automatically remove the duplicates. See below code:
arrString = new HashSet<string>( arrString).ToArray<string>();
Now let’s print the values which are in the array .
foreach(var item in arrString) { Console.WriteLine(“{0}”,item); } //Result is: A, B, C, D, E, FFind more details about HashSet at MSDN http://msdn.microsoft.com/en-us/library/bb341004(v=vs.90).aspx Enjoy the new functionality of the .Net framework. Feel free to ask any issue/doubts related to HashSet. I will try to resolve you all queries.
Comments
Post a Comment
Thanks for your valuable feedbacks.Keep visiting the blog...