given address[] wallets. What is the correct method to check that the list contains a given address?

Does solidity provide any native list contains function?

If not, is the only way to do a full iteration of the array and check each element?

Are there more memory efficient ways of doing this, maybe a mapping?

4 Answers 4


Solidity doesn't provide a contains method, you'd have to manually iterate and check.

Using an array for what you're trying to achieve would be a highly inefficient pattern. The best and most cost efficient method is use a mapping data structure. Set the key to be the address and the value to be a boolean. Lists that are too long have the possibility of running out of gas when you're trying to iterate over them.

If you need to iterate through all the keys in the mapping, then you'd need to have an external database to get all the keys. The database can be populated and updated based on events from the smart contract (i.e. an event when the address is added or removed).


If I were you, I would use a pattern like this:

contract myWallets
    mapping (address => bool) public Wallets;

    function setWallet(address _wallet) public{

    function contains(address _wallet) returns (bool){
        return Wallets[_wallet];

I recommend this answer. However, if you need to use an array for some reason: as a concrete example for a for loop mentioned in Miguel Mota's answer, here's a simple for loop in Solidity that achieves the contains functionality:

bool doesListContainElement = false;
for (uint i=0; i < list.length; i++) {
    if (elementToLookFor == list[i]) {
        doesListContainElement = true;

  • 2
    Nice but I would add a break in the if block
    – Oli Crt
    Commented Jun 23, 2022 at 14:30

If you're starting with an unsorted array and you don't know the index where the data lives then all you can do is loop through it. The gas required to do this grows as the list does, and may become so big that the contract becomes unusable.

The obvious solution is to keep the data in a mapping by address => bool. This gives you a constant lookup cost regardless of the number of items stored.

If the data is sorted then you have a few other options including a binary search, but it's not as clean or simple as a mapping.

One final possibility if you really need to keep the data in an unsorted array is to get the user's software to iterate the array on the client side to find the index of their address, then supply it to the contract as a parameter. Then you just have to check if the data is really at that index. However this gets fiddly if the index changes, because by the time the user's transaction gets to the contract, the data may no longer be where they found it.

  • I wonder if there are MEV bots trying to mess with pre-specified indices to get the user to resend their transaction and pay more gas fees. Of course, MEV wasn't really a thing in september 2017. Commented Oct 10, 2022 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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