Skip to content

Which Data Structure? Explained

Answer a few questions about how you look things up, whether order and uniqueness matter, and whether the relationships are hierarchical or a network to choose between an array, set, map, object, tree, or graph.

A data structure is a shape optimized for certain operations. The choice turns on three questions: how you find a value (by position, by key, by relation), whether duplicates and order matter, and whether the relationships are flat, nested, or a network. Match the structure to the operations you do most. The questions below turn that into a recommendation, ranked against the alternatives.

Decision guide · 6 options

The options

Array

An ordered list indexed by position.

Sequences you read in order, sort, slice, or address by index; the default for lists of items.

Set

A collection of unique values with fast membership tests.

Tracking distinct items, deduplicating, and answering is X present in constant time.

Map

Key-to-value pairs where the key can be any type.

Lookups by an arbitrary key (including objects), counters, and caches that need ordered or non-string keys.

Object / Record

String-keyed pairs, the native record type.

Fixed-shape records and string-keyed lookups where you know the keys ahead of time.

Tree

Nodes with one parent and children, a hierarchy.

Nested, hierarchical data: file systems, DOM, org charts, and recursive structure traversal.

Graph

Nodes connected by arbitrary edges, a network.

Many-to-many relationships: social networks, maps, dependencies, and recommendation graphs.

Which one fits you?

Answer a few questions to get a recommendation.

Question 1 of 4

How do you mostly find a value?