agentleFS
VelimirMuellerVelimirMueller/ai-development-skills/skills/craft/choose-data-structure/SKILL.md

choose-data-structure

Skill7 starsChanged 15 days agoMITView on GitHub

Use when a collection's access pattern is wrong — linear scan, missing index, re-sort, set-versus-list — and you must pick the structure with the right complexity.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

Files
SKILL.md

Choose Data Structure

Picks the collection whose access pattern matches how the code actually uses it (CLRS). Pairs with analyze-complexity — this is the structure half of the same decision.

The table

Access patternStructureCost
Indexed access by positionArray / slice / vectorO(1)
Keyed lookup, no order neededHash table / map / dictO(1) average
Ordered or range queries, min/maxBalanced BST / sorted treeO(log n)
Peek min or max, push and popHeap / priority queueO(1) peek, O(log n) push/pop
Insert/delete at a known positionLinked listO(1) after the node, O(n) to find it
Membership with orderingSorted array + binary searchO(log n) search, O(n) insert
Set of unique keysSetO(1) membership

1. Audit (change nothing)

cat .claude/stack-profile.md 2>/dev/null | head -20
grep -rn "\.find(\|\.includes(\|\.indexOf(\|\.sort(\|\.filter(" --include="*.ts" --include="*.rs" --include="*.go" --include="*.py" . 2>/dev/null | head -30

Name the actual access: is the code searching for one element repeatedly (→ map/set, not array), does it need the smallest repeatedly (→ heap), does it re-sort the same list (→ keep it sorted), does it test membership on a list (→ set)? Each mismatch is a change, not a rewrite.

2. Match structure to use

  • Repeated find/includes on an array → build a map or set once, look up in O(1).
  • Repeated min/max over an unsorted list → a heap.
  • "Does this value exist?" on a list → a set.
  • Ordered iteration over a changing sorted list → a balanced tree or a sorted structure that stays sorted.
  • A fixed-size lookup table keyed by a small enum → a plain array indexed by the enum.

3. State the trade-off

A hash map buys O(1) lookup and gives up order; a tree buys order and gives up the constant factor; an array buys cache locality and gives up keyed lookup. Name which property the code needs and which it gives up, and keep the simpler structure when both work.

Verify

The repeated linear scan is gone (one O(1) lookup replaces it), the re-sort is gone (the structure stays sorted), and the chosen structure's complexity matches the named access pattern. State the before/after in the note.

When to deviate

  • Bounded collections (a handful of items): the constant factor of a map may cost more than a linear scan.
  • The structure is the framework's own (a query cache, a store) — use it, do not hand-build a replacement.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.