Generate a function f() which maps a known set to another known set

32 Views Asked by At

I have a set S of random positive integers that is of known size n. Is there a way to generate a hashing function which perfectly maps each value of S uniquely onto the set P where P consists of values 0 to n-1.

For example:

Given:

S = { 345, 69205, 0, 8237, 512 }

P = { 0, 1, 2, 3, 4 }

a valid function f could be

f(345) = 2, f(69205) = 4, f(0) = 3, f(8237) = 3, f(512) = 1

Note that the ordering of the mapping need not match the ordering of the elements of set S, that is, 345 need not map to 0. While the given example function f is valid, it would not be the only valid mapping.