How to Choose the Right Data Structure

A good data structure does not merely store information. It makes the operation you care about cheap, clear, and difficult to misuse.

When a solution feels complicated, the problem is often not the algorithm yet. It is the way the data is represented. Start by asking what the program must do most often.

Start with the key operation

Do you need fast indexed access, membership checks, ordered processing, repeated minimum retrieval, or relationships between objects? The answer narrows the choices quickly.

Array or list: access by position

Use an array or list when order matters and you frequently access items by index. Iteration is simple and cache-friendly. Appending is often efficient, but inserting near the front may require shifting many elements.

Good for: sequences, tables, buffers, and collections processed in order.

Hash set or map: membership and lookup

Use a set when the main question is “Have I seen this value?” Use a map when a key must lead to a value, count, or record. Average lookup is constant time, but the structure uses extra memory and does not naturally express sorted order.

Good for: counting frequencies, deduplication, caching, and matching identifiers to records.

Stack: last in, first out

A stack removes the most recently added item first. This mirrors nested work: the latest unfinished task must be completed before returning to the earlier one.

Good for: parsing brackets, undo systems, depth-first traversal, and explicit recursion.

Queue: first in, first out

A queue processes items in arrival order. It is the natural model for fair scheduling and level-by-level exploration.

Good for: breadth-first search, task processing, event pipelines, and simulations.

Heap or priority queue: repeated best choice

A heap keeps the smallest or largest priority item easy to remove without fully sorting everything. It is especially useful when new candidates keep arriving.

Good for: schedulers, top-k problems, shortest-path algorithms, and merging sorted streams.

Tree: hierarchy and ordered search

Trees represent parent-child structure. Specialized balanced search trees support ordered insertion, lookup, and range queries. Tries organize strings by shared prefixes.

Good for: file systems, syntax trees, indexes, autocomplete, and hierarchical categories.

Graph: relationships and routes

Choose a graph when connections are as important as the items themselves. An adjacency list is usually compact for sparse graphs; an adjacency matrix makes direct edge checks simple but may use much more space.

Good for: roads, dependencies, social links, networks, recommendations, and state transitions.

A quick decision sequence

  1. Write down the operations the program performs.
  2. Identify which operation dominates runtime or complexity.
  3. Choose the simplest structure that makes that operation efficient.
  4. Check the memory cost and whether ordering is required.
  5. Test the structure against empty, duplicate, and maximum-size inputs.

Do not optimize the wrong thing

A hash map is not automatically better than a list, and a tree is not automatically more sophisticated than an array. The best choice is contextual. If a collection has five items and is scanned once, a straightforward list may be clearer and faster in practice than a more elaborate structure.

Choose for the real workload, document the tradeoff, and measure when performance matters.


Keep the major choices nearby with the free Algorithms Made Simple Quick Reference, then continue with the full guide on the book page.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top