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).
11 Tallypoints 16 Alphanumeric 19 Roman 34 Alphabets 46 Numbers
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 ). 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- What are Hash Functions and How to choose a good Hash Function?
- Overview of Data Structures | Set 2 (Binary Tree, BST, Heap and Hash)
- String hashing using Polynomial rolling hash function
- Sorting using trivial hash function
- Cryptographic Hash Function in Java
- Difference between Data Scientist, Data Engineer, Data Analyst
- Advantages of BST over Hash Table
- Implementing our Own Hash Table with Separate Chaining in Java
- Hash Table vs STL Map
- Implementing own Hash Table with Open Addressing Linear Probing in C++
- Sort elements by frequency | Set 4 (Efficient approach using hash)
- Graph representations using set and hash
- Generating hash id's using uuid3() and uuid5() in Python
- MD5 hash in Java
- C# | How to get hash code for the specified key of a Hashtable
- Getting the hash code of the ValueTuple in C#
- Getting the Hash Code of the Specified Range in C#
- Getting the Hash Code of the Given Index in C#
- Difference between Data Warehousing and Data Mining
- Primitive data type vs. Object data type in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.