Mappings and arrays are the two main ways to store collections in Solidity. A mapping is a key-value lookup table, such as an address to a balance, that gives constant-cost access but cannot be listed. An array is an ordered list of elements that you can index and loop over, either fixed-size or dynamic. The solidity mapping array pair covers almost every collection you will store in a beginner contract.
This guide uses Solidity 0.8.x and assumes you know state variables and functions.
A mapping is declared as mapping(KeyType => ValueType). Every possible key exists and maps to the default value of the value type, which is 0 for numbers, false for booleans and the zero address for address. Nothing is stored until you assign a value.
Reading balanceOf[someAddress] for an address that never deposited returns 0, not an error. The mapping stores values at a slot computed from a hash of the key, which is why lookups cost the same no matter how many entries exist.
Mappings have important restrictions:
address, uint256 or bytes32, and also string or bytes. Structs, arrays and other mappings cannot be keys.delete m[key] resets one entry to its default value.A value can be another mapping, which is how an ERC-20 allowance is modelled: the owner maps to a spender, and the spender maps to an amount.
A fixed-size array has its length in the type, such as uint256[3]. A dynamic array, such as uint256[], can grow and shrink in storage.
Storage arrays support push(value), pop() and .length. Indexing past the end reverts with a panic error, so out-of-range reads are safe but fail loudly. Arrays in memory have a fixed length chosen with new uint256[](n) and have no push or pop.
Solidity has no built-in remove. delete numbers[i] only resets that slot to 0 and leaves a gap. If order does not matter, use the swap-and-pop pattern, which is cheap.
If order must be kept, you have to shift every later element down by one, which costs gas proportional to the array size.
| Feature | Mapping | Array |
|---|---|---|
| Lookup by key | Constant cost | By index only; searching costs a loop |
| Iterate all entries | Not possible | Yes, but gas grows with length |
| Know the size | No | length |
| Memory use | Storage only | Storage or memory |
| Best for | Balances, ownership, flags | Ordered lists, small sets |
A common pattern combines both: a mapping for fast lookup and an array of keys for listing.
Looping over a storage array that users can grow is a classic mistake. Each iteration costs gas, and a transaction has a gas limit, so a long enough array can make the function impossible to call, which is a denial-of-service risk. Keep loops over user-growable arrays out of state-changing functions, or process the array in batches. Reading a big array in a view function from outside the chain is fine, because nodes run it locally.
No. Keep an array of keys beside the mapping if you need to iterate, or index the data off-chain from events.
The default value of the value type, such as 0 or false. This means you cannot tell a missing key from a stored default unless you add a flag.
Mappings are usually cheaper for lookups and writes by key. Arrays cost more as they grow because of the length slot and element shifts.
No. Return the value you need, or return an array copied to memory.
Use a mapping when you look up values by key, such as balances, and an array when you need order, a length or iteration. Mappings return defaults for unset keys and cannot be listed, while storage arrays support push, pop and length. Combine both for lookup plus listing, and never loop over an array that users can grow without a limit.