Junior — Middle
How quickly can an element be retrieved if you access it by its position in the array?
sobes.tech AI
Answer from AI
Accessing an array element by its position occurs in constant time — O(1). This is because arrays store elements in a contiguous memory area, and the address of the desired element is calculated using the formula: base address + index * size of element. For example, in Kotlin or Java, you can access an element like this:
val array = arrayOf(10, 20, 30, 40)
val element = array[2] // Fast access to the element with value 30
Such access is very efficient and independent of the array size.