I am trying to pair two unique values, lets call them A and B, there is no mathematical correlation between them. When having value A I want to know value B and vice versa. I thought of creating two mappings:

mapping( uint32 => byte32 ) toB;
mapping( byte32 => uint32 ) toA;

However I will need to store each value twice, is there another solution to this ?


If I understand well, you want to store values and keys, but the value is also the key of the key?

If so, according to my knowledge, there is no "proper" way to achieve this.

You could check the gas consuption of the following solutions:

Solution 1: As you said, two mappings

Solution 2: Only one mapping, but you loop through the keys until you find the value you are looking for and return the key. But I think this solution will be too costly in terme of gas if you have a lot of values. Thanks to @smarx who pointed out that it is not possible

Not sure if this is helpfull, but interesting question.

  • 2
    Regarding solution 2, I believe there's no way to iterate over the keys of a Solidity mapping.
    – user19510
    Nov 23 '17 at 2:54
  • Indeed, forgot about that, corrected the answer. Nov 23 '17 at 2:58
  • Yes, you will probably be forced to use an A to B mapping, and a B to A mapping. Good question!
    – Alex
    Nov 23 '17 at 4:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.