0

I have an uint256[] array. I need to check that no value is repeated in the array more than once.

What is an efficient way to do this?

porton
  • 1,774
  • 5
  • 17
  • 38

1 Answers1

1

A lot depends on whether you will need to remove a number from the array. Let us assume no, because it wasn't mentioned.

You have an array of values. In addition, a mapping of values to bools, which you require are false on before appending a new value, and set to true as you go.

mapping(uint => bool) public exists;

Hope it helps.

p.s. This an other common patterns are described over here: Are there well-solved and simple storage patterns for Solidity?

Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145