This section explains how to remove an array element in Solidity.
Syntax
To remove an array element in Solidity, use the pop method.
Note that the element to be removed is the last element in the array.
array.pop();
Sample Code
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Test {
uint[] public array = [1, 2, 3];
function removeLastElement() public {
// Remove an array element
array.pop();
}
function getArray() public view returns(uint[] memory) {
return array;
}
}
Keep this in mind along with the method for adding an array (push method).