Guys how should i remove and element with filling the gap if the array contains only the element that im deleting??And because of that removing an element with shifting doesnt work. What other ways could i use to delete an element filling the gap??


uint[] public arr;

function modifyArray(uint[] memory _arr) public {

function remove(uint num) public {
    for(uint i=0;i < arr.length; i++){
      if(arr.length ==1 || i+1 == arr.length) {

       else {
         arr[i] = arr[i +1];


1 Answer 1


You really have four options: Create a new array and assign the parts of old array in the order you want

Use temp variables and reassign the elements of you array

Push new elements to the end your array via _array.push(element)

Pop the last element of your array until you get the achieved result via _array.pop(element) and the push the part you want after that

Or a combination of these.

  • ok ill figure out whats the best way thanks!
    – zzz
    Feb 7, 2022 at 18:27
  • wait i could just have an if statement that check if the length is 1 then just do pop() but if its more then replace the gap with the last element right?? Or the pop() is going to leave the 0x0000000000000 ??
    – zzz
    Feb 7, 2022 at 18:49
  • Could you give me the example? pop frees the memory on the EVM but you no longer can access that memory location. pop on an array of length 1 results in an empty array (length 0).
    – FudgyDRS
    Feb 7, 2022 at 18:57
  • ok i added an example i think it shoud work
    – zzz
    Feb 7, 2022 at 19:08
  • From the code you provided this would be the same effect: function remove() public { require(arr.length==1); arr.pop();}
    – FudgyDRS
    Feb 7, 2022 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.