0

I have the following array of numbers:

1. [2, 4, 6, 8]

2. [4, 2, 5, 7]

I need to check whether or not each number of the first array is included in the second array. In that case, a function needs to return true. If at least one number of the first array is not included in the second array, the function needs to return false.

I can do this by comparing each pairs of numbers, but that doesn't seem the most computationally efficient function. What would be the most gas-efficient way to achieve this in Solidity?

UPD: The first array numbers are function inputs, the second array numbers are global variables.

  • 1
    Are these global (state-variable) arrays, or local (function argument) arrays? If they're global, then most likely the method that you've mentioned would be the cheapest, as the other option - sorting each one of the arrays - would be computationally faster (O(n*log n) instead of O(n^2)), but requires changing state variables, which would ultimately consume a lot of gas. – goodvibration Oct 14 '18 at 12:39
  • Thank you, I should have clarified that. The first array numbers are function inputs, the second array numbers are global variables. – Ruham Oct 14 '18 at 12:42
  • Do you need the result to be written back to the state data, or are you just returning it to the caller? – Richard Horrocks Oct 14 '18 at 17:25
  • I need to return "true" or "false". – Ruham Oct 14 '18 at 19:08
1

Simply get the first number from the input array and check it against the second array numbers, from first to fourth, if you do not find it, break and return false. If you find it, break and pass to the second of the first array.

When you finished numbers in the first array and any number of the first array map to some of the second, you can return true.

It can be demonstrated that this is the more efficient way to check them against your constraints.

Hope this helps!

0

If you can, maintain a mapping for the global array?

mapping (uint => bool) exists

Then the check would simply be looping through the function arg array.

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.