Mappings & Arrays
Mappings and arrays are the primary data structures in Solidity. Mappings are hash tables optimized for O(1) key lookup; arrays support iteration. Understanding their gas costs shapes every contract design.
Mappings
mapping(KeyType => ValueType) — Solidity's hash table
Properties:
- O(1) read/write — extremely gas efficient for key lookup
- Keys are never stored — only their keccak256 hash is used as storage slot
- Default value — unset keys return the type's default (0, false, address(0))
- Cannot iterate — no way to enumerate all keys natively
- Cannot delete all — only delete(mapping[key]) works per-entry
Nested mappings:
mapping(address => mapping(address => uint256)) — used by ERC-20 allowances
Workaround for iteration: maintain a parallel array of keys.Arrays
Fixed-size arrays: uint256[10] data — size known at compile time, stored in-place
Dynamic arrays: uint256[] data — size grows with push/pop, stored with length slot + elements
Key operations:
- arr.push(x) — appends element, returns reference
- arr.pop() — removes last element (does not return it)
- arr.length — current length
- delete arr[i] — sets element to 0/default but does NOT shrink length!
- For memory arrays: uint256[] memory arr = new uint256[](length) — fixed size, no push/pop
Storage layout: A dynamic array at slot S stores length at S, elements at keccak256(S) + iCommon Mistakes
- Iterating over large arrays on-chain — unbounded loops can exceed the block gas limit, permanently locking contract functions. Always paginate or use off-chain enumeration.
- Using delete arr[i] expecting it to shrink the array — delete only zeroes the element. Use swap-with-last + pop for true removal.
- Returning a storage array to memory — large storage arrays returned from view functions can be very gas-expensive. Consider pagination or event-based approaches.
- Not validating array index bounds — Solidity 0.8.x reverts on out-of-bounds array access, but this consumes all remaining gas. Validate index < arr.length first.
Tip
Tip
Practice Mappings Arrays in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Once deployed, smart contracts are immutable — code is law
Practice Task
Note
Practice Task — (1) Write a working example of Mappings Arrays from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Mappings Arrays is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready web3 code.
Key Takeaways
- Mappings and arrays are the primary data structures in Solidity.
- Iterating over large arrays on-chain — unbounded loops can exceed the block gas limit, permanently locking contract functions. Always paginate or use off-chain enumeration.
- Using delete arr[i] expecting it to shrink the array — delete only zeroes the element. Use swap-with-last + pop for true removal.
- Returning a storage array to memory — large storage arrays returned from view functions can be very gas-expensive. Consider pagination or event-based approaches.