Open In App

Hash Function for String data in C#

Last Updated : 25 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Question: Write code in C# to Hash an array of keys and display them with their hash code. 

Answer:  Hashtable is a widely used data structure to store values (i.e. keys) indexed with their hash code. Hash code is the result of the hash function and is used as the value of the index for storing a key. If two distinct keys hash to the same value the situation is called a collision and a good hash function minimizes collisions. 

Question: How to choose a hash function appropriate for the data? 

Answer: If your data consists of integers then the easiest hash function is to return the remainder of the division of key and the size of the table. It is important to keep the size of the table as a prime number. But more complex functions can be written to avoid the collision. If your data consists of strings then you can add all the ASCII values of the alphabets and modulo the sum with the size of the table (the code below depicts the same). 

To achieve a good hashing mechanism, It is important to have a good hash function with the following basic requirements: 

1. Collisions are resistant. Collisions occur when pairs of elements are mapped to the same hash value. These should be avoided. Note: Irrespective of how good a hash function is, collisions are bound to occur. 

2. Fast to compute: The hash of a string of length n should be computable in at most O(n) time else the whole purpose of hashing the string will be defeated. 

3. Easy to compute: It should be easy to compute and must not become an algorithm in itself. 

4. Uniform distribution: It should provide a uniform distribution across the hash table and should not result in clustering.

Example 1: 

Csharp




// C# Program to create a Hash
// Function for String data
using System;
 
class Geeks {
     
    // Main Method
    public static void Main(String []args)
    {
         
        // Declaring the an string array
        string[] values = new string[50];
        string str;
 
        // Values of the keys stored
        string[] keys = new string[] {"Alphabets",
            "Roman", "Numbers", "Alphanumeric",
                                "Tallypoints"};
 
        int hashCode;
 
        for (int k = 0; k < 5; k++) {
             
            str = keys[k];
             
            // calling HashFunction
            hashCode = HashFunction(str, values);
 
            // Storing keys at their hashcode's index
            values[hashCode] = str;
        }
 
        // Displaying Hashcodes along with key values
        for (int k = 0; k < (values.GetUpperBound(0)); k++) {
             
            if (values[k] != null)
                Console.WriteLine(k + " " + values[k]);
        }
    }
 
    // Defining the hash function
    static int HashFunction(string s, string[] array)
    {
        int total = 0;
        char[] c;
        c = s.ToCharArray();
 
        // Summing up all the ASCII values
        // of each alphabet in the string
        for (int k = 0; k <= c.GetUpperBound(0); k++)
            total += (int)c[k];
 
        return total % array.GetUpperBound(0);
    }
}


Output

11 Tallypoints
16 Alphanumeric
19 Roman
34 Alphabets
46 Numbers

Example 2: 

C#




// C# Program to create a Hash
// Function for String data
using System;
 
class Geeks {
     
    // Main Method
    public static void Main(String []args)
    {
         
        // Declaring the an string array
        string[] values = new string[50];
        string str;
 
        // Values of the keys stored
        string[] keys = new string[] {"C", "C++",
                "Java", "Python", "C#", "HTML"};
 
        int hashCode;
 
        for (int k = 0; k < 5; k++) {
             
            str = keys[k];
            hashCode = HashFunction2(str, values);
 
            // Storing keys at their hashcode's index
            values[hashCode] = str;
        }
 
        // Displaying Hashcodes along with key values
        for (int k = 0; k < (values.GetUpperBound(0)); k++) {
             
            if (values[k] != null)
                Console.WriteLine(k + " " + values[k]);
        }
    }
 
    // Defining the hash function
    static int HashFunction2(string s, string[] array)
    {
        long total = 0;
        char[] c;
        c = s.ToCharArray();
 
        // Horner's rule for generating a polynomial
        // of 11 using ASCII values of the characters
        for (int k = 0; k <= c.GetUpperBound(0); k++)
         
            total += 11 * total + (int)c[k];
 
        total = total % array.GetUpperBound(0);
         
        if (total < 0)
            total += array.GetUpperBound(0);
 
        return (int)total;
    }
}


Output

6 C#
15 C++
18 C
19 Python
28 Java

Explanation: 

In HashFunction, we pass the arguments as a string to be hashed and the string data ‘values‘. The method ToCharArray converts the string to the character array and then we start a for loop from the start to the end of the character array. Inside the for loop we calculate the sum total of the ASCII values of each character in the array. The method GetUpperBound returns the value of the highest index of the array. 

Then hash function returns modulo of the sum total by the upper bound of the array ( in this case 49 since string[] values = new string[50] ). Whereas in HashFunction2, we are passing the same arguments but this function is less likely to have collisions. Everything is basically the same except here we use Horner’s Rule to compute the polynomial function of 11.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads