Solidity theorytheory 0/50 · 0%
Data · medium

28. Structs, arrays and mappings together

Modelling records without unbounded loops.

Combine a mapping for O(1) lookup with an array only when you truly need enumeration — and remember arrays cost gas to iterate and can grow past the block gas limit.

struct Position { uint128 amount; uint64 since; bool active; }
mapping(address => Position) public positions;
address[] public stakers;

Deleting from the middle of an array is expensive; the swap-and-pop trick moves the last element into the gap when order does not matter.

Check your understanding

  1. 1. What is the risk of iterating a growing array on chain?

  2. 2. What does swap-and-pop do?

  3. 3. Why pack a struct into uint128/uint64?