github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/execution/testdata-shanghai/remove-from-array.sol (about)

     1  // SPDX-License-Identifier: GPL-3.0
     2  
     3  pragma solidity ^0.8.14;
     4  
     5  contract Contract {
     6      uint256[] public values;
     7  
     8      function find(uint256 value) internal view returns (uint256) {
     9          uint256 i = 0;
    10          while (values[i] != value) {
    11              i++;
    12          }
    13          return i;
    14      }
    15  
    16      function removeByValue(uint256 value) public {
    17          uint256 i = find(value);
    18          removeByIndex(i);
    19      }
    20  
    21      function removeByIndex(uint256 i) public {
    22          while (i < values.length - 1) {
    23              values[i] = values[i + 1];
    24              i++;
    25          }
    26          values.pop();
    27      }
    28  
    29      function getValues() internal view returns (uint256[] storage) {
    30          return values;
    31      }
    32  
    33      function test() public returns (uint256[] memory) {
    34          values.push(10);
    35          values.push(20);
    36          values.push(30);
    37          values.push(40);
    38          values.push(50);
    39          removeByValue(30);
    40          uint256 i = find(40);
    41          removeByIndex(i);
    42          return getValues();
    43      }
    44  }