In this Solidity tutorial, we will illustrate the use of the Solidity Array with multiple examples. Additionally, we will discuss the following set of topics.
- Solidity Array
- Solidity Array push
- Solidity Array length
- Solidity Array of structs
- Solidity Array functions
- Solidity Array pop
- Solidity Array methods
- Solidity Array of string
- Solidity Array of mappings
- Solidity Array remove element
Solidity Array
An array in computer programming is a group of data elements stored in the same memory region at the same time. We can also consider it as a basic data structure where each data element may be accessed simply by its index value.
There are many circumstances in programming where we need to store a huge volume of data of the same type. However, defining multiple variables to hold such a large amount of data is very difficult.
Rather, we can create an array and store all of the components within it.
Now, Solidity also facilitates the arrays to its developers. Solidity supports the use of fixed-size and dynamic-sized arrays.
- A fixed-size array in Solidity is one whose size is already defined and cannot hold elements more than its size.
- On the other hand, a dynamic size array in Solidity is one whose size is not already defined, and its actual size is determined at the runtime.
Next, let’s discuss how to create and define these different types of arrays in Solidity. But, before that, let’s look at the general syntax of declaring and initializing an array.
<data_type> <arr_name>[size] = <initialize_array>
- In the above syntax, first, we need to define the data type of the array in place of data_type.
- After this, we need to specify the name of the array in place of arr_name.
- Next, based on the requirement, we can define the size of the array within the square brackets [].
- In the end, we can also choose to initialize the array by assigning some values.
Now, let’s discuss some examples related to each type of array in solidity.
Example 1: Solidity fixed-size array
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract FixedSizeArr {
uint[5] data = [17, 27, 37, 47, 57];
function getArray() public view returns (uint[5] memory) {
return data;
}
function getArrData(uint x) public view returns (uint) {
return data[x];
}
}
- In the above example, first, we have created a contract named FixedSizeArr.
- Within the contract, we have declared and initialized a fixed-size unit type array named data with a maximum size of 5 elements.
- After this, we created 2 functions, first is getArray which will return the complete data array.
- And second is the getArrData function that will return a single array element based upon the given index value.

In the above execution, first, we are using the getArray function to get the complete value of the data array. After this, we are using the getArrData function to fetch the array element at index 2.
Example 2: Solidity dynamic-size array
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract DynamicSizeArray {
string[] cities = ["Chicago", "Dallas", "Phoenix", "Houston"];
function getCities() public view returns (string[] memory) {
return cities;
}
function getCity(uint x) public view returns (string memory) {
return cities[x];
}
}
- In the above example, first, we have created a contract named DynamicSizeArray.
- Within the contract, we have declared and initialized a dynamic-size string type array named cities.
- After this, we created 2 functions where getCities will return the complete cities array.
- And we also created a getCity function that will return a single array element based upon the given index value.

In the above execution, first, we are using the getCities function to get the complete value of the cities array. After this, we are using the getCity function to fetch the array element at index 2 i.e. Houston.
Read: Solidity Constructor
Solidity Array Functions
Till now, we understood how to create arrays in Solidity. In this section, we will illustrate some of the common functions or methods associated with arrays in Solidity.
Solidity Array Push
In the previous section, we have seen how to create and initialize arrays with certain values. But, how can we add new elements within the existing arrays?
For this task in Solidity, we generally utilize the push method on arrays. The main role of the push operation is to append new elements at the last of an array.
Let’s understand how to implement the push operation on an array in Solidity using an example.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract ArrayPush {
string[] countries = ["United States", "Canada"];
function getCountries() public view returns (string[] memory) {
return countries;
}
function AddCountry(string memory name)
public { countries.push(name); }
}
- In the above example, first, we have created a contract named ArrayPush.
- Within the contract, we have defined a dynamic-size string type array named countries. Additionally, we have initialized the array with 2 values.
- After this, we created the getCountries function that will return the array value.
- In the last, we created another function named AddCountry, the purpose of this function is to accept a string value and insert it at the last of the countries array. For this task, we are using the countries.push method.
The execution of the above smart contract is shown below.

In the above execution, first, we have specified “New Zealand” as input for the AddCountry function and executed it. After this, we are using the getCountries function to fetch the value of the array. As result, we can observe that the New Zealand value is appended at the last of the array.
Solidity Array Length
Here we will discuss how to get the length of an array in Solidity. For this task in Solidity, we utilize the length method with an array.
While working with a dynamic-size array, the size of an array is not defined. And in such situations, we can employ the Array.length method in Solidity which returns the length of an array of type uint.
Let’s understand how to get the length of an array in Solidity using an example. And for this, consider the following code.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract ArrayLength {
string[] countries = ["United States", "Canada"];
function getCountries() public view returns (string[] memory) {
return countries;
}
function AddCountry(string memory name)
public { countries.push(name); }
function getArrLen() public view returns (uint) {
return countries.length;
}
}
- In this example, first, we created a contract named ArrayLength. Within this contract, we defined and initialized a string type array named countries.
- After this, the contract consists of 3 functions.
- First is the getCountries function that will return the array value.
- Second is the AddCountry function that will accept a string value and insert it at the last of the countries array.
- In the last, there is the getArrLen function that will return the length of the array using the countries.length method.
The execution of the above smart contract is shown below.

In the above execution, first, if we call the getArrLen function, it will return 2 as a value because we initialized the countries array with 2 values.
Now, we are using the AddCountry function to append Australia in the countries array. In the last, we can use the getArrLen and getCountries function to check if the name is added or not. Moreover, the length of the array will become 3.
Solidity Array Pop
Similar to the push operation in an array, we can also implement the pop operation in Solidity. The role of pop operation is to remove an element from the last position of the array. And for this implementation, we use the pop method in the Solidity array.
For better understanding, let’s discuss the pop operation and the use of a pop method with the help of an example in Solidity.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract PopOperation {
uint[] data = [19, 29, 39, 49, 59];
function getArray() public view returns (uint[] memory) {
return data;
}
function PushData(uint val)
public { data.push(val); }
function PopData()
public { data.pop(); }
}
- In this example, first, we created a contract named PopOperation. Within this contract, we defined and initialized a uint type array named data.
- After this, the contract consists of 3 functions.
- First is the getArray function that will return the entire value of the data array.
- The second is the PushData function that will accept a uint value and insert it at the last of the data array.
- In the last, there is the PopData function that will remove the last element from the data array using data.pop() method.
Next, we will compile and deploy the contract, and here is the execution of the smart contract.

In this execution, when we use the getArray function for the first instance, it will return a uint type array with 5 values. And if we run the PopData function, it will delete the last element from the array which is 59. After this, if we again try to fetch the array, it will return only 4 elements.
Read: Solidity Function + Examples
Solidity Array of String
In this section, we will illustrate how to create a string-type array in solidity. So, as discussed in the first section, arrays in Solidity contain data of the same type.
Now, Array of String type simply means an array in solidity which holds only string values. In Solidity, we can easily create an array of string data type by using the following syntax.
string <arr_name>[size] = <initialize_array>
- In the above syntax, first, we need to define the data type of the array which is going to be a string.
- After this, we need to specify the name of the array in place of arr_name.
- Next, based on the requirement, we can define the size of the array within the square brackets [].
- In the end, we can also choose to initialize the array by assigning some values.
Solidity Array of String Example
Next, for better understanding, let’s execute an example in Solidity using the above syntax.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract USA_Cities {
string[] cities = ["Chicago", "Dallas", "Phoenix", "Houston"];
function getCities() public view returns (string[] memory) {
return cities;
}
function AddCity(string memory city_name)
public { cities.push(city_name); }
function getArrLength() public view returns (uint) {
return cities.length;
}
}
- In this example, first, we created a contract named USA_Cities. Within this contract, we defined and initialized a string type array named cities.
- After this, we created 3 functions with the USA_Cities function.
- First is the getCities function that will return the complete value of the array.
- Second is the AddCity function that will accept a string value and insert it at the last of the cities array.
- In the last, there is the getArrLength function that will return the length of the array using the cities.length method.

Initially, if we use the getCities function, it will return a string type array with 4 values in it. Now, in the execution, we use the AddCity function to insert “New York” as a string value within the cities array.
After this, we can check the entire array using the getCities function. Moreover, we can also check the length of the cities array using the getArrLength function.
Read: Solidity String
Solidity Array of Structs
The Solidity programming language supports various built-in data types like string, integers, and even address. However, solidity also enables us to create a user-defined data type commonly known as a struct. So, by using structs in solidity, we can create our data type based on the requirements.
And in this section, we will illustrate how to create an array in the solidity of the struct data type. For this, first, we should know how to create a struct type in Solidity. The syntax of creating a struct is shown below.
struct name_of_struct {
datatype1 type_1;
datatype2 type_2;
.
.
.
}
In the above syntax, first, we need to define the name of the structure with the struct keyword. Now, within the structure, we can define variables of different data types. Once the struct is created and defined, we can use it to create an array of the same type.
Solidity Array of Structs Example
Let’s discuss the implementation using an example in Solidity.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract info {
struct User {
string name;
bool isActive;
}
User[] public users;
function addUsers(string memory username, bool active) public {
users.push(User({name: username, isActive: active}));
}
function getUserStruct() public view returns(User[] memory) {
return users;
}
}
- In the example, first, we created a contract named info and within the contract, we defined a struct named User.
- This struct consists of 2 variables, the first is the name variable of string type and the second is the isActive variable of boolean data type.
- After creating a struct, we created an array of struct named users. Next, we created a function named addUsers whose role is to accept a string and a bool type value and append it to the array.
- In the last, we created another function named getUserStruct and this function will return the complete users array.

In the above execution, first, we can utilize the addUsers function to insert some data into the array. After this, we can use the getUserStruct function to fetch the entire array. Moreover, we can also use the users getter method to get the name and isActive value at a certain index.
Solidity Array of mappings
Mapping in Solidity is similar to a dictionary available in many different programming languages. Additionally, Mapping holds data in a key-value pair. However, there is one constraint while using mapping in Solidity, we cannot iterate through mappings in solidity.
First, let’s discuss how to create a mapping in Solidity. For this task, we will discuss its syntax first which is as follows.
mapping(key => value) <access_specifier> <name>;
In the above syntax, the key represents the key of the mapping and it can be of any data type except for the mapping data type itself. On the other hand, the value represents the value associated with a key and it can also be of any data type including array and mapping type.
After specifying the data type for key and value, the next step is to define the name of the mapping and also specify the access specifier for it.
Next, let’s understand how to use this syntax with the help of an example in Solidity. Here we will be creating a mapping with values as an array type.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract USA_states {
mapping(string => string[]) stateCities;
function addStateCities(string memory state_name, string memory city_name)
public {
stateCities[state_name].push(city_name);
}
function getStateCities(string memory state_name)
public view returns (string[] memory) {
return stateCities[state_name];
}
}
- In the example, first, we created a contract named USA_states and within the contract, we defined a mapping named stateCities.
- This mapping consists key of string type which will represent the state name from the United States. And the value of the key is an array of a string data type that will represent the city names under the specified state.
- After defining the mapping, we created a function named addStateCities that will take state and city names as input and adds them to the mapping.
- After this, we created another function named getStateCities. This function will take the state name as input and return the array of all the city names within that state.

In the above execution, we are first using the addStateCitiies function to add some cities located in Texas. And then we are using the getStateCities function to fetch the array associated with the Texas key.
Solidity Array remove element
In this section, we will discuss how to remove an element from an array in solidity. Generally, solidity does not facilitate any built-in function to remove an array element directly.
So, for this implementation, we are manually going to create a function that will take the array index as input and remove the element from that given index.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract RemoveArrayElement{
uint[] data = [31,72,93,14,35];
//function to get data array
function getData() public view returns (uint[] memory) {
return data;
}
//function to remove element from data array
function removeData(uint x) public{
delete data[x];
}
//functop to get lenght of data array
function getArrLength() public view returns(uint){
return data.length;
}
}
- In the above code, we created a solidity smart contract named RemoveArrayElement. Within this contract, we created and initialized a dynamic-size array with 5 array elements.
- After this, we created multiple functions where the getData and getArrLength function is used to fetch the array value and array length respectively.
- Moreover, we also created a function named removeData which takes the index as input and removes the array element from that particular index,
However, there is one issue in the execution of this smart contract.

When we remove an element from the data array using the delete method, it deletes the value and sets the default value which is 0. Due to this, the length of the array also remains the same.
So, to remove elements, we need to bring the elements one slot up and then remove the last one. For this implementation, the example code is given below.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
contract RemoveArrayElement{
uint[] data = [31,72,93,14,35];
function getData() public view returns (uint[] memory) {
return data;
}
function removeData(uint x) public{
for(uint i = x; i < data.length-1; i++){
data[i] = data[i+1];
}
data.pop();
}
function getArrLength() public view returns(uint){
return data.length;
}
}
In the above code, we have modified the removeData function where we are using a for loop to bring each array element one slot up and remove the last one by using the pop() method.
Here is the correct execution of the above example.

So, in this Solidity tutorial, we have illustrated the use of Solidity Arrays with multiple examples. Additionally, we have discussed the following set of topics.
- Solidity Array
- Solidity Array push
- Solidity Array length
- Solidity Array of structs
- Solidity Array functions
- Solidity Array pop
- Solidity Array methods
- Solidity Array of string
- Solidity Array of mappings
- Solidity Array remove element
I am Bijay, a Microsoft MVP and founder of TSInfo Technologies, a SharePoint development company. Currently focusing on getting expertise on Ethereum, Solidity, Bitcoin, Cryptocurrency, Blockchain, etc. Sharing my expertise and tutorials on Bitcoin and Ethereum related technologies. Read More…