How to Check If Value Exists In Array Using Solidity

Spread the love

Are you doing Solidity programming? Want to check if value exists in array using Solidity?

Let’s get started!

How to check if the value exists in array using Solidity

To express a sequence of fixed body sizes, the programmer determines the type of elements and the number of elements needed in a row as follows:

type arrayName [ arraySize ];

This is called a one-dimensional array. ArraySize must always be an integer greater than zero, and the type can be any valid Solidity data type. For example, to explain a 10-element field named equilibrium uint, use this command –

uint balance[10];

To express the sequence of the dynamic size of Solidity, the programmer determines the type of elements as follows:

type arrayName [ arraySize ];

Initializing Arrays

You can run the Solidity field elements individually or use a command such as the following:

uint balance[3] = [1, 2, 3];

The number of values ​​between square brackets [] cannot be greater than the number of elements we have declared for the field between square brackets []. The following is an example of assigning an element to an array.

If your ship is the size of a row, the row is just big enough to hold the beginning. So as you write –

uint balance[] = [1, 2, 3];

You can create the same field as in the previous example.

balance[2] = 5;

The above command gives the number of the 3rd element in the field the value 5.

Checking Array Elements

The element can be accessed by indexing the field name. This is done by placing the index of the element in square brackets after the field name. For example –

uint salary = balance[2];

The above statement takes over the 3rd element of the line and states the amount of the salary variable. The following is an example of all the three concepts mentioned above, ie command, task, and access to length-length returns the line size. length can be used to resize the dynamic range that places it.

Push allows you to add an element to the dynamic storage field at the end. Returns the new length of the field.

An example code is given below:

pragma solidity ^0.5.0;


contract test {

   function testArray() public pure{

      uint len = 7; 


      //dynamic array

      uint[] memory a = new uint[](7);


      //bytes is same as byte[]

      bytes memory b = new bytes(len);


      assert(a.length == 7);

      assert(b.length == len);


      //access array variable

      a[6] = 8;


      //test array variable

      assert(a[6] == 8);


      //static array

      uint[3] memory c = [uint(1) , 2, 3];

      assert(c.length == 3);



Read further: How to use msg.sender in Solidity Code

Leave a Comment