Back to Benchmark

Array Iteration - O(n) Complexity

Understanding why iterating through all elements in an array is linear time

What is Array Iteration?

Array iteration means visiting each element in an array exactly once, typically in order from first to last. Unlike random access where you jump directly to any element, iteration requires going through elements one by one.

Common Use Cases:

  • Searching: Finding an element by value (linear search)
  • Processing: Performing an operation on each element
  • Summing/Counting: Calculating totals or counting elements
  • Transformation: Creating a new array based on existing elements

Think of it like reading a book page by page - you can't skip ahead to page 50 without reading pages 1-49 first. You must visit each page in sequence.

How Does Array Iteration Work?

1. Start from the Beginning

Begin at index 0 (the first element) and process it.

2. Move to Next Element

Increment the index and process the next element. Repeat until you reach the end of the array.

3. Visit Every Element Once

To process all n elements, you must visit each one exactly once:

Array with 10 elements → 10 visits
Array with 100 elements → 100 visits
Array with n elements → n visits
Time complexity: O(n)

⚠️ Key Difference from Random Access

Random access (O(1)) lets you jump directly to any element. Iteration (O(n)) requires visiting elements in sequence. If you need to process all elements, iteration is necessary and optimal!

Interactive Array Iteration Visualization
Click the button below to see how array iteration works step by step!

Code Example:

// Iterating through an array - O(n)
const arr = [10, 20, 30, 40, 50];

// Method 1: For loop
for (let i = 0; i < arr.length; i++) {
  // Process arr[i] - O(1) per element
  console.log(arr[i]);
}
// Total: O(n)

// Method 2: For...of loop
for (const value of arr) {
  console.log(value); // O(n)
}

// Method 3: forEach
arr.forEach((value) => {
  console.log(value); // O(n)
});
Built-in Functions for Array and String Iteration
Most programming languages provide built-in functions for iterating through arrays and strings

Why is Array Iteration O(n)?

1. Must Visit Every Element

To process all elements, you have no choice but to visit each one. There's no way to skip elements if you need to process them all.

2. Linear Relationship

The time taken is directly proportional to the number of elements:

10 elements → 10 operations
100 elements → 100 operations
1,000 elements → 1,000 operations
n elements → n operations
Time = n × constant = O(n)

3. Cannot Be Optimized Further

If you need to process every element, O(n) is optimal. You cannot do better than visiting each element once. This is the best possible time complexity for this operation.

💡 When O(n) is Optimal

O(n) might seem slow compared to O(1) or O(log n), but when you need to process all elements, O(n) is the best you can do! It's optimal for operations that require examining every element.

Time Complexity Comparison
OperationTime ComplexityExplanation
Array Iteration (all elements)O(n)Must visit each element once - optimal for this operation
Array Random AccessO(1)Access single element by index - instant
Linear SearchO(n)Search for value - may need to check all elements
Binary Search (sorted)O(log n)Search in sorted array - eliminates half at each step
String IterationO(n)Iterate through all characters - same as array iteration
Real-World Examples

📊 Calculating Sum

To sum all numbers in an array, you must visit each element once to add it to the total. This is O(n) and cannot be done faster.

🔍 Finding Maximum

To find the maximum value in an unsorted array, you must check every element. This is O(n) - you can't know the maximum without seeing all values!

🔄 Transforming Data

When converting an array of temperatures from Celsius to Fahrenheit, you must process each element. This requires O(n) time.

📝 String Processing

Counting vowels in a string, reversing a string, or checking if all characters are uppercase - all require visiting each character once, making them O(n) operations.

🎨 Image Processing

Applying a filter to every pixel in an image requires iterating through all pixels. For an image with n pixels, this is O(n).

Key Takeaways
  • Array iteration is O(n) - linear time

    Time is directly proportional to the number of elements. Double the elements, double the time.

  • O(n) is optimal when processing all elements

    If you need to examine every element, O(n) is the best possible time complexity - you cannot do better!

  • Different from random access

    Random access (O(1)) lets you jump to any element. Iteration (O(n)) requires visiting elements sequentially.

  • Essential for many operations

    Many common operations (sum, max, transform, search) require iteration and are therefore O(n).

Practice Exercises
Test your understanding of array iteration with these exercises. Try to solve them yourself before checking the answers!