Array shifts

When an array element is removed using delete, it does not shrink the array length. This leaves a gap in the array. Here we introduce an technique to shrink the array after removing an element.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

contract ArrayShift {
    uint[] public arr = [1, 2, 3];

    function remove(uint _index) external {
        // Write your code here
        require(_index < arr.length, "index out of bound");
        for(uint i = _index;i< arr.length -1 ;i++){
            arr[i]=arr[i+1]
        }
        arr.pop
    }
}

Last updated

Was this helpful?