Chapter 3 · Linear Structures

Arrays

The simplest and most universally-used data structure. A contiguous block of memory where every element lives at a predictable address — making index-based access O(1) and the foundation of nearly every other structure.

What is an array data structure?

An array stores items in a contiguous, ordered block of memory, each with a numeric index starting at 0. Reading or writing by index is O(1), and adding or removing at the end is O(1) amortized. Inserting or removing at the start or middle is O(n), because every later element has to shift.

const nums = [10, 20, 30];
nums[1];                // 20     O(1)
nums.push(40);          // end    O(1) amortized
nums.pop();             // end    O(1)
nums.unshift(5);        // start  O(n): shifts everything
nums.includes(30);      // search O(n)

How arrays live in memory

An array is a contiguous reservation of memory. If each integer takes 4 bytes and the array starts at address 1000, then element at index i is always at address 1000 + (i × 4). No searching required — pure arithmetic.

100x03E8
250x03EC
70x03F0
430x03F4
20x03F8
880x03FC

Six 4-byte integers in contiguous memory. Highlighted: index [2] at address 0x03F0.

🔍Why O(1) access works
The CPU doesn't need to traverse any structure. It just computes one address and reads from it — a single memory fetch. This is the fastest possible access pattern and why arrays are the default choice when you know the index.

Live demo — try the operations

Each box below is an element. The index is shown beneath it. Click each operation and watch how the array changes — notice which ones shift all elements (expensive!) vs. only touch the end.

// Interactive Array — length: 7

10
[0]
25
[1]
7
[2]
43
[3]
2
[4]
88
[5]
31
[6]
addremoveaccessmodify
idx:

→ Click an operation to see it in action

Core operations

Four operations define almost everything you do with an array. The cost of each one flows directly from the contiguous-memory model.

array-operations.pseudo
1// ACCESS by index — O(1)
2get(arr, i):
3 return memory[arr.base + i × arr.elementSize]
4 
5// SEARCH — O(n) unsorted, O(log n) sorted
6linearSearch(arr, target):
7 for i from 0 to arr.length - 1:
8 if arr[i] = target: return i
9 return -1
10 
11// INSERT at end — O(1) amortized
12push(arr, val):
13 if arr.length = arr.capacity: resize(arr, capacity × 2)
14 arr[arr.length] = val
15 arr.length += 1
16 
17// INSERT at index k — O(n)
18insertAt(arr, k, val):
19 shift elements arr[k..n-1] one position right // O(n)
20 arr[k] = val
21 arr.length += 1

Time complexity breakdown

Access by indexO(1)

Constant time

Push to end (amortized)O(1)

Constant time

Pop from endO(1)

Constant time

Insert at beginning / middleO(n)

Linear time

Delete at beginning / middleO(n)

Linear time

Linear search (unsorted)O(n)

Linear time

Binary search (sorted)O(log n)

Logarithmic time

Static vs. dynamic arrays

Most languages give you dynamic arrays (Python's list, Java's ArrayList, C++'s vector). They appear unlimited but internally double their capacity when full. Here's exactly what happens:

1. Array is full

Capacity = 4. You push a 5th element. The array can’t grow in place — nearby memory may be in use.

2. Allocate new block

A new block of capacity × 2 = 8 slots is reserved elsewhere in memory.

3. Copy + free

All 4 existing elements are copied to the new block. The old block is freed. This one-time cost is O(n).

4. Insert the new element

Now arr[4] = newValue. We’re back to O(1). Doubling ensures resizes happen so rarely that the amortised per-push cost remains O(1).

💡Why double and not +1?
If you grew by just 1 slot each time, every push would trigger a copy: costs of 1+2+3+…+n = O(n²) total for n pushes. Doubling means you copy at sizes 1, 2, 4, 8…: total cost 1+2+4+…+n = 2n = O(n) for n pushes, or O(1) per push amortised.

When to use an array

YES

Random access by index

If you frequently access elements by position, nothing beats O(1) index lookup.

YES

Dense, sequential data

Storing a leaderboard, pixel buffer, or time-series measurements — arrays are cache-friendly.

YES

Iteration over all elements

Arrays are the most cache-efficient structure for sequential traversal — CPU prefetching works perfectly.

NO

Frequent insert/delete in the middle

Every middle insertion shifts n elements. Use a linked list or deque instead.

NO

Unknown or highly variable size

If you expect massive growth/shrinkage patterns, consider a linked list or hash map.

🎯 Quick Check

An array has 1,000 elements. You call deleteAt(0) to remove the first element. What is the time complexity?

⚠️The cache advantage
Arrays win over linked lists in practice more often than theory predicts, because modern CPUs load memory in 64-byte cache lines. When you access arr[0], arr[1]…arr[15] are all pulled into the cache simultaneously. Linked list nodes scattered in memory cause a cache miss on every single node. Benchmarks routinely show arrays 5–10× faster for iteration even though Big O says they’re equal.

Arrays: summary

Key points

  • Use arrays when you need ordered data and fast access by position.
  • Searching an unsorted array is O(n); a sorted array allows O(log n) binary search.
  • In JavaScript, arrays grow automatically (they're dynamic arrays).
  • Prefer non-mutating methods (map, filter, toSorted) when other code shares the array.

Frequently asked questions

›What is the time complexity of array operations?

Access by index O(1), push and pop O(1), shift and unshift O(n), insert or delete in the middle O(n), search O(n) (unsorted).

›Why are arrays zero-indexed?

The index is an offset from the start of the array's memory: the first element is 0 positions from the start.