pragma solidity ^0.4.24;
contract test{
uint[] public array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
function removewithgap(uint index) returns(uint[]) {
if (index >= array.length) return;
delete array[index];
return array;
}
function removewithoutgap(uint index) returns(uint[]) {
if (index >= array.length) return;
for (uint i = index; i<array.length-1; i++){
array[i] = array[i+1];
}
delete array[array.length-1];
array.length--;
return array;
}
}
When I am using removewithgap
function my transaction cost is kind os normal.
But when I use removewithoutgap
function my transaction cost increases exponentially as the length of my array grows. Is there any efficent way to remove the element without gap?
increases exponentially
, I believe that you meanincreases linearly
. – goodvibration May 22 '20 at 11:42