I have an array of strings. Array has length n. How to compute hash key for each string, so each key will be a number in range of 0..n?
UPDATE
Array’s items could be not strings, but numbers if it will helps to someone to help me 😉
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You cannot choose a hash function for this without first looking at contents of the array. Suppose you pick a hash function and let me pick the array. I generate 2n strings, apply the hash function, and sort the result. With 2n strings and only n possible values there must be collisions, so I pick n strings that include lots of collisions and give them back to you to hash and observe the collisions.
If you are prepared to do the work of analysing the strings ahead of time to choose your hash function one starting point – or source of search terms – would be “Minimal perfect hash function” in http://en.wikipedia.org/wiki/Perfect_hash_function.
You could also consider whether this is really what you want, and whether you could consider using a less perfect hash function. I like the look of http://en.wikipedia.org/wiki/Cuckoo_hashing, myself.