01 / 20

Big-O for
Arrays

Understand how loops and common array operations scale with input size \( n \): from constant-time lookups to quadratic nested loops.

JavaScript

What is Big-O Notation?

Big-O describes how the number of operations grows as the input size \( n \) increases. It answers the question: "If my array doubles in size, how much slower does my code get?"

Why Does Efficiency Matter?

\( O(1) \) Lookup
Access index 5 in a 1,000,000-element array: 1 step
\( O(n) \) Search
Find a value in 1,000,000 elements: up to 1,000,000 steps
\( O(n^2) \) Nested
Compare every pair in 1,000,000 elements: 1,000,000,000,000 steps

Visualizing Growth Rates

Input size n Operations 0 25 50 75 100 \( O(1) \) \( O(\log n) \) \( O(n) \) \( O(n \log n) \) \( O(n^2) \)

\( O(1) \): Constant Time

An operation is \( O(1) \) when it takes the same number of steps regardless of the array's size. The array could have 10 or 10 million elements: the cost is identical.

const arr = [10, 20, 30, 40, 50]; // Access by index: O(1) const val = arr[2]; // 30, instant // push() to end: O(1) arr.push(60); // adds to end // pop() from end: O(1) arr.pop(); // removes last

\( O(n) \): Linear Time

An operation is \( O(n) \) when it must visit each element once. Doubling the array size doubles the work.

// Searching for a value: O(n) function findValue(arr, target) { for (let i = 0; i < arr.length; i++) { if (arr[i] === target) return i; } return -1; }

\( O(n^2) \): Quadratic Time

Nested loops that both depend on \( n \) produce \( O(n^2) \) complexity. Each element is compared against every other element.

// Find all duplicate pairs: O(n²) function hasDuplicate(arr) { for (let i = 0; i < arr.length; i++) { for (let j = i + 1; j < arr.length; j++) { if (arr[i] === arr[j]) return true; } } return false; }

push/pop vs shift/unshift

0 1 2 3 4 5 A B C D E F push pop O(1) unshift shift O(n) Every element must shift to a new index

The Loop Counting Rule

1
Count the nested loops that depend on \( n \). A single loop over \( n \) elements is \( O(n) \).
2
Multiply nested levels. Two nested loops each running \( n \) times gives \( n \times n = O(n^2) \).
3
Add sequential blocks. A loop of \( O(n) \) followed by another \( O(n) \) is \( O(n) + O(n) = O(n) \): constants drop.
4
Drop constants and lower terms. \( O(2n + 100) \) simplifies to \( O(n) \). Only the fastest-growing term matters.

How to Analyze Any Function

1
Identify the input: what is \( n \)? Usually the array's .length.
2
Find every loop (for, while, .forEach, .map, .filter, etc.) and note what it iterates over.
3
Check for nesting: is there a loop inside a loop? If both scale with \( n \), that level multiplies.
4
Check for hidden loops: methods like .includes() inside a loop add another \( O(n) \) layer.
5
Take the dominant term and express it in Big-O: \( O(1) \), \( O(n) \), \( O(n^2) \), etc.

Example 1: Sum an Array

function sum(arr) { let total = 0; // O(1) for (const num of arr) { // O(n) total += num; // O(1) per iteration } return total; // O(1) }

Example 2: Hidden Nested Loop

function removeDupes(arr) { const result = []; for (const item of arr) { // O(n) if (!result.includes(item)) { // O(n) hidden! result.push(item); } } return result; }

Example 3: Sequential Loops

function processData(arr) { // Loop 1: double each element const doubled = arr.map(x => x * 2); // O(n) // Loop 2: filter positives const pos = doubled.filter(x => x > 0); // O(n) // Loop 3: sum them return pos.reduce((a, b) => a + b, 0); // O(n) }

Array Methods Cheat Sheet

Method Big-O Why
arr[i] \( O(1) \) Direct index access
push / pop \( O(1) \) Modify end only
shift / unshift \( O(n) \) Re-indexes all elements
splice (middle) \( O(n) \) Shifts elements after target
indexOf / includes \( O(n) \) Linear scan worst case
map / filter / reduce \( O(n) \) Visits every element
sort \( O(n \log n) \) Comparison-based sort

Common Mistakes

✕ Ignoring Hidden Loops
Calling .includes() or .indexOf() inside a loop creates a hidden \( O(n^2) \). Use a Set for \( O(1) \) lookups.
✕ Using shift() in a Loop
Processing a queue with shift() inside a while-loop is \( O(n^2) \). Consider reversing and using pop() instead.
✕ Confusing Sequential vs Nested
Two separate \( O(n) \) loops are not \( O(n^2) \). They are \( O(n) + O(n) = O(n) \). Only nesting multiplies.

Question 1 of 5

What is the time complexity of accessing arr[500] in an array of 10,000 elements?

Question 2 of 5

A function uses a for-loop that calls .indexOf() on the same array during each iteration. What is the overall time complexity?

Question 3 of 5

Why is .unshift() considered \( O(n) \) while .push() is \( O(1) \)?

Question 4 of 5

A function has three consecutive loops: .map(), then .filter(), then .reduce(). Each processes the full array. What is the total Big-O?

Question 5 of 5

What is the time complexity of mystery()?