Skip to main content

What is the time complexity of accessing an element in an array?

The time complexity of accessing an element in an array is O(1) (constant).


Why

An array is stored in a contiguous memory area, and each element has a fixed size. To find the element at index i, the processor simply calculates its address using the formula:

[ \text{address} = \text{start_address} + i \times \text{element_size} ]

That is, access requires no traversal or search, just one arithmetic operation and one memory access.


What this means

  • It doesn't matter how many elements are in the array, 10 or 10 million, the time to get any element is the same.
  • That's why operations like arr[i] run in constant time.

Important

  • O(1) is the ideal case for access.
  • But searching by value (for example, "find the number 42 in the array") is already O(n), because every element has to be checked.

Summary:

Accessing an array element by index runs in O(1), instantly, regardless of the array's size.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.