# How to Delete an Element from an Array in Solidity?

Deleting an element from an array in solidity can be an easy job, but some tricky parts lie under the hood. We use the “`delete`” function to remove the element from an array in solidity. But the length of the array and its sequence may not remain in the way we are expecting. Let’s go for a case study.

## Remove element with delete()

We will create an array called `firstArray` and a function called `removeItem()` which will delete a specific item from an array.

```pragma solidity ^0.8.0;
contract itemRemoval{
uint[] public firstArray = [1,2,3,4,5];
function removeItem(uint i) public{
delete firstArray[i];
}
function getLength() public view returns(uint){
return firstArray.length;
}
}
```

Here we have created an array of integers and two functions, “`removeItem(")` and “`getLength()`” inside the contract.

• `firstArray` is an array of integers
• `removeItem()` function deletes an item from a specific index
• `getLength()` function measures the length of the array.

Let’s deploy the contract and check the length of the `firstArray`. The result is “5”, as clearly visible that the “`firstArray`” consists of five numbers inside it. Now let’s remove the number 3 from the array. Since the indexing of an array starts from 0, the number 3 is in index two of the `firstArray`.

As we can see, we have removed the item from the `firstArray`, but the length of the array remains the same.

## Why did the length of the array not change?

So what’s going on? We put the delete function, and it looks like it worked. So how come the length of the array is not changing. The answer follows:

When we run the `delete` function, actually, we are not removing the number from the array. We are just removing the value of that index and switching it to the default value “0”. So, the length of the array will literally remain the same, but the index two will be zero. From the picture above, we can see when we called the 2nd index of the `firstArray`; it returned 0.

Note: Always remember, when you delete an item from an array, the length remains the same in Solidity.

## Get the actual length of the array keeping the array unordered

There is a simple solution to this problem. We can create a function `remove()` that will take an array’s index as an argument. Then the function will set the index argument to the last element in the array. Simply, we will shift the index argument over the last element of the array. Then we will remove the last element by using the `pop()` method. It may seem unclear at first look. Let’s try the code.

```function remove(uint index) public{
firstArray[index] = firstArray[firstArray.length - 1];
firstArray.pop();
}
```

Here, the `remove()` function takes an argument “`index`” and this index is moved at last by (`firstArray.length-1`). Our desired value is now shifted as the last element of the array. We need to eliminate the value by using the `pop()` method. The `pop()` method has been introduced in Solidity recently, and it will automatically decrease the length of the array by eliminating the last element. In some older versions of Solidity, “`firstArray.length--`” is used instead of `pop()`.

After deploying the contract, we wanted to remove 3, index 2, from the array. As we checked the array’s length, we got the result 4, and that’s perfect. But wait a minute, as we reviewed the value of index 2 in the array, we got 5. But according to the array sequence, it should be ‘4’ as we removed ‘3’ from the array. This function is speedy and efficient, but there is a problem. It leaves the array unordered. That’s why we are getting ‘5’ at index ‘2’ instead of 4. That is one of the limitations of this function. So, how can we keep the order of the array unchanged? Yes, there is a solution. We will check it out now.

## Get the actual length of the array keeping the array in order

In this method, we will use a for loop to bring the elements of the array one slot up. For instance, If we want to remove ‘3’ from the array, we will start iterating from index two. And gradually bring up index three in the position of index two and index four in index three and follow the procedure if any more elements are available in the array. It will leave the last index of the component empty, and we will delete this vacant position at the end of the algorithm. Let’s proceed with the code.

```function orderedArray(uint index) public{
for(uint i = index; i < firstArray.length-1; i++){
firstArray[i] = firstArray[i+1];
}
firstArray.pop();
}
```

Here we have written one for loop where we defined “i” as an index that will iterate until the array’s second last element. We defined this with (`firstArray.length-1`). We have changed the index of the array with the logic (`firstArray[i] = firstArray[i+1]`). At last, we used the `pop()` method to remove the last empty element from the array.

We removed the 2nd index, the number 3, from the array. Next, we checked the 2nd index from the `firstArray`, and we got ‘4’ because the next element of the array is 4. It also returned the length of the array perfectly. So this method enables us to find a perfect way to get an array with elements in order.

But in comparison with the earlier method, there are more steps involved in this one. So naturally, it will consume more gas than the previous one. That’s why we should use this method only when keeping the elements of the array in order is absolutely required.

For further study, you can go through the following links:

## Learn Solidity Course

Solidity is the programming language of the future.

It gives you the rare and sought-after superpower to program against the “Internet Computer”, i.e., against decentralized Blockchains such as Ethereum, Binance Smart Chain, Ethereum Classic, Tron, and Avalanche – to mention just a few Blockchain infrastructures that support Solidity.

In particular, Solidity allows you to create smart contracts, i.e., pieces of code that automatically execute on specific conditions in a completely decentralized environment. For example, smart contracts empower you to create your own decentralized autonomous organizations (DAOs) that run on Blockchains without being subject to centralized control.

NFTs, DeFi, DAOs, and Blockchain-based games are all based on smart contracts.

This course is a simple, low-friction introduction to creating your first smart contract using the Remix IDE on the Ethereum testnet – without fluff, significant upfront costs to purchase ETH, or unnecessary complexity. 