Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Data-Structure Selection Guide

Choose a data structure from the operation that must be cheap, not from its name or theoretical reputation.

Ordered sequences

NeedStart with
Indexed sequence, stack, contiguous sliceVec
Queue or active operations at both endsVecDeque
Fixed-capacity streamingRing buffer
Node-based links or whole-list joiningLinkedList
Rolling minimum or maximumMonotonic deque

Keys, membership, and order

NeedStart with
Key to value, order irrelevantHashMap
Unique membership, order irrelevantHashSet
Key to value with ranges and sortingBTreeMap
Unique membership with ranges and sortingBTreeSet
Dense bounded integer membershipBit set
Compact negative membership filterBloom filter
Prefix lookupTrie

Priority, identity, and relationships

NeedStart with
Repeated greatest or smallest itemBinaryHeap
Change arbitrary prioritiesIndexed priority queue
Stable IDs with slot reuseGenerational arena
Arbitrary relationshipsAdjacency-list graph
Connectivity under edge additionsUnion-find
Lookup plus recencyLRU composition
Dynamic overlap queriesInterval tree

Questions to ask

  1. Is access by position, key, priority, range, prefix, identity, or recency?
  2. Which operations dominate, and what are their required bounds?
  3. Is order semantic or merely convenient for display?
  4. Is the domain dense or sparse?
  5. Are identities stable across removal and reuse?
  6. Must memory be bounded?
  7. Does contiguous layout outweigh asymptotic differences?
  8. What invariant must every mutation preserve?
  9. Can a simpler structure meet the measured workload?

Final rule

Prefer the simplest representation whose invariants make the required operations cheap enough—and verify the decision with realistic measurements.