Arrays are comparable to mapping. In Solidity, mapping functions similarly to a hash table or dictionary in any other language. These are used to store data in the form of key-value pairs, where the key can be any of the built-in data types but not reference types, and the value can be any type. Mappings are commonly used to link a unique Ethereum address to the related value type. Whereas Structure allows users to create and define their own type in the form of solidity. The structure is a collection of multiple kinds, even if it cannot contain a member of its own type. The structure is a variable of the reference type that can include both value and reference types.

Example for Mapping

Mapping (address => uint)  public myMapping;

Example for Structure

Struct Payments{
Uint amount;
Uint timestamp;
}

Struct Balance{
Uint totalBalance;
Uint numPayaments;

Mapping(uint => Payments) payments;

}
Mapping(address => balance) public balanceReceived;

Mapping

Contract SimpleMappingExample{

Mapping (uint => bool) public myMapping;
Mapping(address => bool) public myAddressMapping;

Function  setvalue(uint _index) public{

myMapping[_index]=true;
}

Function setMyAddressToTrue() public{

myAddressMapping[msg.sender]=true;

Address Keys in Mapping

In setMyAddressToTrue() function, It uses global msg.sender to obtain the sender address. So, if you're engaging with a specific address, that address will be in msg.sender in the smart contract. And then it access the myAddressMapping and sets the value to “true” for the current address.

Structure

  1. Structure helps us to create our own custom variable types.
  2. Members of the struct cannot be of the type struct.
  3. it is better to define structs than objects to reduce gas consumption.
struct People {
        uint256 favoriteNumber;
        string name;
    }   

    function store(uint256 _favoriteNumber) public {
        favoriteNumber = _favoriteNumber;
    }
contract Storage {

    uint256 favoriteNumber;

    struct People {
        uint256 favoriteNumber;
        string name;

    }
    // uint256[] public anArray;

    People[] public people;
    mapping(string => uint256) public nameToFavoriteNumber;

    function store(uint256 _favoriteNumber) public {

        favoriteNumber = _favoriteNumber;

    }
    function retrieve() public view returns (uint256) {
        return favoriteNumber;

    }
    function addPerson(string memory _name, uint256 _favoriteNumber) public {
        people.push(People(_favoriteNumber, _name));
        nameToFavoriteNumber[_name] = _favoriteNumber;
    }
}

In this way structure and mapping can be used in an efficient manner.