Arrays and Their Operations — Data Structures

An array stores items side by side with no gaps, with indexes (position numbers) starting at 0. Reading by index (arr[2]), reading length (the array's…

The people in one row of cinema seats sit close together, with no empty seat between them. Someone who comes in late and sits at the end of the row bothers nobody. But if they want to sit in the middle, everyone to their right has to move one seat to make room.

An array works like that row of seats. An array keeps its items in boxes that sit side by side with no gaps, one item in each box. The boxes are the seats, and the items are the people. Each item has a position number (its index) starting at 0. In this lesson you'll see how much work each operation needs: reading, adding, inserting and removing.

Five operations on one array

The array arr starts as [10, 20, 30, 40, 50], then these five operations run in order:

Step Code What moves arr afterwards
1 arr[2] nothing (gives 30) [10, 20, 30, 40, 50]
2 arr.push(60) nothing [10, 20, 30, 40, 50, 60]
3 arr.splice(3, 0, 35) 40, 50, 60 move right [10, 20, 30, 35, 40, 50, 60]
4 arr.splice(0, 1) 6 items move left [20, 30, 35, 40, 50, 60]
5 arr.pop() nothing [20, 30, 35, 40, 50]

Here are the same five operations as code:

const arr = [10, 20, 30, 40, 50];

console.log(arr[2]);  // 30
arr.push(60);         // [10, 20, 30, 40, 50, 60]
arr.splice(3, 0, 35); // [10, 20, 30, 35, 40, 50, 60]
arr.splice(0, 1);     // [20, 30, 35, 40, 50, 60]
arr.pop();            // [20, 30, 35, 40, 50]
console.log(arr);     // [20, 30, 35, 40, 50]

Fast at the end, slow in the middle

An array can't have gaps. So the amount of work depends on where the operation happens:

Programmers write this amount of work in Big O notation (a short way of writing it), which the next lesson covers. O(1) means the number of steps stays the same, however long the array is. O(n) means the number of steps grows with the number of items, written n: with twice the data, it takes roughly twice the time.

Operation Example Big O
Read by index arr[2] O(1)
Read the array's length arr.length O(1)
Add or remove at the end push, pop O(1)
Insert or remove in the middle or at the start splice, unshift, shift O(n)
Search for a value includes, indexOf O(n)

So an array fits best for ordered data that you often read by index, and where you add or remove items at the end.

💡 Why can reading by index jump straight there? An array's items are stored right next to each other in memory. The computer knows the address of the first item (the number of its place in memory). So the address of the item at index i is one calculation: the address of the first item plus i times the size of one item. It stays one calculation, however long the array is. Now and then push needs extra work: when the space set aside for the array is full, all its items are moved to a bigger space. But on average, each push is still O(1).

⚠️ What makes an array slow, or delete too much

TL;DR: An array stores items side by side with no gaps, with indexes (position numbers) starting at 0. Reading by index (arr[2]), reading length (the array's length), and push and pop at the end are all O(1): the number of steps stays the same, however long the array is. Inserting or removing in the middle or at the start (splice, unshift, shift) and searching for a value (includes) are O(n): the number of steps grows with the number of items. The reason is that the other items have to move, or are checked one by one. arr.splice(i, 1) removes one item at index i, and arr.splice(i, 0, x) inserts x at index i.

⚡ Read, and add at the end

Write:

const arr = [10, 20, 30, 40, 50];

console.log(arr[2]);  // 30
arr.push(60);         // [10, 20, 30, 40, 50, 60]
Guess first: why does arr[2] give 30 and not 20?

Indexes (the position numbers of an array's items) start at 0. So index 2 is the third item, 30.

Meaning: arr[2] reads the item at index 2. push adds at the end, and pop removes the last item. No other item is touched, so the number of steps stays the same, however long the array is. Programmers write this as O(1).

⚡ splice: insert and remove

Write:

arr.splice(3, 0, 35); // [10, 20, 30, 35, 40, 50, 60]
arr.splice(0, 1);     // [20, 30, 35, 40, 50, 60]

Meaning: arr is the array from the step before. In splice, the first number is the position (an index), the second is how many items to remove, then comes the value to insert. splice(3, 0, 35) inserts 35 at index 3, and splice(0, 1) removes one item at index 0.

⚡ The middle is O(n)

Guess first: splice(3, 0, 35) runs on an array of a million items. Which items move?

Every item from index 3 to the end, each one index to the right. The longer the array, the more items move.

Meaning: Inserting or removing in the middle or at the start (splice, unshift, shift) moves other items, and searching for a value (includes) checks the items one by one. The number of steps grows with the number of items (n), so these are written O(n). Reading by index, length, push and pop stay O(1).

⚡ Watch out for splice without the second number

Meaning: arr.splice(2) removes every item from index 2 to the end of the array, not just one. Write arr.splice(2, 1) when you want to remove only one item.

What you will learn