Iterable mapping
Mapping is not iterable in Solidity.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract IterableMapping {
mapping(address => uint) public balances;
mapping(address => bool) public inserted;
address[] public keys;
function set(address _addr, uint _bal) external {
balances[_addr] = _bal;
if (!inserted[_addr]) {
inserted[_addr] = true;
keys.push(_addr);
}
}
function get(uint _index) external view returns (uint) {
address key = keys[_index];
return balances[key];
}
function first() external view returns (uint) {
// Write your code here
address firstKey = keys[0];
return balances[firstKey];
}
function last() external view returns (uint) {
// Write your code here
address lastKey = keys[keys.length-1];
return balances[lastKey];
}
}
Last updated
Was this helpful?