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.
Six 4-byte integers in contiguous memory. Highlighted: index [2] at address 0x03F0.
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
→ 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.
1// ACCESS by index — O(1)2get(arr, i):3 return memory[arr.base + i × arr.elementSize]45// SEARCH — O(n) unsorted, O(log n) sorted6linearSearch(arr, target):7 for i from 0 to arr.length - 1:8 if arr[i] = target: return i9 return -11011// INSERT at end — O(1) amortized12push(arr, val):13 if arr.length = arr.capacity: resize(arr, capacity × 2)14 arr[arr.length] = val15 arr.length += 11617// 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] = val21 arr.length += 1
Time complexity breakdown
Constant time
Constant time
Constant time
Linear time
Linear time
Linear time
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).
When to use an array
Random access by index
If you frequently access elements by position, nothing beats O(1) index lookup.
Dense, sequential data
Storing a leaderboard, pixel buffer, or time-series measurements — arrays are cache-friendly.
Iteration over all elements
Arrays are the most cache-efficient structure for sequential traversal — CPU prefetching works perfectly.
Frequent insert/delete in the middle
Every middle insertion shifts n elements. Use a linked list or deque instead.
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?
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.