Search code examples
mathmappingunique

Pair function that generate unique value for a given number of integers


I have various sets of integers each set can have from 2 to >10 integers with values between 0 and 500ish (variable). I would like to pair them into a unique number. I have explored the Cantor pairing function, but I would have to combine two numbers at a time and for longer groups of number it would soon result in very large numbers.

For example: set 1: [1,12,65,4] will be mapped to a unique value different for the value representing set 2: [1,12,65,2].

Also, another nice to have requirement would be that sets [1,4,78,5] and [1,4,78,10] would be close to each other when represented by a unique number.

Is this possible mathematically?


Solution

  • If I understand your question correctly, you want an injective function R^n -> R. Yes, this is definitely possible.

    The easiest solution would be just to string the digits together. For example, for [1,12,65,4] each digit can be represented as [001,012,065,004] and you could map this to 1012065004. This also has the property that it is close to [1,12,65,2] -> 1012065002.

    Another solution is to "interweave" the digits. For example, if you have [abc,def] -> adbecf. So for [1,12,65,4] -> [001,012,065,004] -> '000001601254' -> 1601254. This results in smaller values.

    Once you have this injective function, you can compose it with another injective function R -> R (e.g. f(x) = log x) to make the values not too large for your application.