Sobes.tech
Junior — Middle

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

sobes.tech AI

Answer from AI

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

This is because arrays in memory are stored as a contiguous block, and access to an element by index is done directly through address calculation: the base address of the array plus an offset depending on the size of the element and the index.

For example, in Swift or Objective-C, arrays are implemented so that index access is instantaneous, without iterating through elements.

What is the time complexity of accessing an element… - sobes.tech