How to Remove item from array in Solidity

Spread the love

Arrays are data structures that store the fixed collection of elements of the same data types in which every element has a specific location called index. Rather than defining multiple individual variables of the same type, we declare a single array of the desired size and store the components in the array, which can then be used for accessing using the index. An array in Solidity can be either fixed or dynamic in size.

Arrays store data in a contiguous memory location, with the lowest index corresponding to the first element and the highest index corresponding to the last.

How to Remove item from array in Solidity

The field size must be set in advance. The total number of elements in the array should not exceed the array’s size. If no line size is specified, the line is large enough to hold the beginning.

Dynamic Array

The field size is not predefined as explained. As the elements grow, the size of the array changes and the size of the array is determined at run time. Here is a quick guide for you How to use msg.sender in Solidity Code?

Array elements can be accessed using an index. You must have access to the (i-1) index to access this entry.

The Pop function

Pop is used when the last element of an array is removed from each dynamic array.

Example: In the example below, the contract types first start with the [data] field, and then the values ​​from the field are extracted using the pop-up function.

// Solidity program to demonstrate

// Pop operation

pragma solidity ^0.5.0;

// Creating a contract

contract Types {

// Defining an array

uint[] data

= [10, 20, 30, 40, 50];

// Defining a function to

// pop values from the array

function array_pop(

) public returns(uint[] memory){

data.pop();

return data;

}

}

Leave a Comment