Dot Net For All

Searches made Fast using HashSet in C#

Hello Friends, everyone works with collections in programming. But to know the correct use of each and every collection type provided by the .NET framework is very important for the good programming practice. That is why I will discuss the use to HashSet in C# with code examples and their utilization.

How HashSet in C# are made

Hash set are made using the hash table. A hash method takes data (like a string, or a file’s contents) and outputs a hash, a fixed-size string or number.

For example, here’s the MD5 hash (MD5 is a common hash method) for a file simply containing “cake”:

DF7CE038E2FA96EDF39206F898DF134D

And here’s the hash for the same file after it was edited to be “cakes”:

0E9091167610558FDAE6F69BD6716771

In C# we can find the Hash of string using the HashAlgorithm class present in System.Security.Cryptography

Below is one Example:

public static byte[] GetHash(string inputString)
{
    HashAlgorithm algorithm = MD5.Create();  //or use SHA256.Create();
    return algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
}

Each and every distinct string has a different hash. And the same concept is used to store the values in HashSet.

Same concept is used to create the Dictionary in .NET framework where the hash is computed for the key element.  And Key can be of any data type.

HashSet and Array Differences

HashSet in C# or for that matter in any programming language are made of on top of arrays.

Arrays also provide constant time to lookup any element, but the lookup is based on index of the element. If we know the index of the element we can get the element in constant time.

But what if we don’t know the index and still want to retrieve the element in constant time. HashSet comes to our rescue. HashSet computes the key for the element we are storing in the array.

Moreover the we can store any type of element in the HashSet for example string. And hashing algorithm calculates the hash of the element.

To look up the value for a given key, we just run the key through our hashing function to get the index to go to in our underlying array to grab the value.

HashSet Example in C#

Below is code example with HashSet and array example in C#

        public static void HashSet()
        {
            HashSet<int> hashSet = new HashSet<int>();
            hashSet.Add(1);
            hashSet.Add(2);
            hashSet.Add(3);
            hashSet.Add(1);

            int[] intArray = new int[] {1,2,3,1 };

            Console.WriteLine(hashSet.Count);
            Console.WriteLine(intArray.Count());               
        }

And is we see the output of the above code.

And I believe you should be aware why this happened. Because I added 1 twice to HashSet, but HashSet only stored one item only once because the Hash calculation for the same item would be same.

Now if we want to find any item in the above collection using Contains method, the time complexity for the item search in HashSet would be constant. But in the array it would be linear i.e. O(n) as it would iterate over all the items one by one to search the particular item.

That was all about the HashSet in C# with some code example and time complexity. And if you are very peculiar about the search time for items in collection you can go for HashSet.

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview