Big-O of Inserting Element at Specific Index in an Array

Last updated on 6 Dec, 2022

Here is a code that inserts an item to specific index. Here, there is no pushing or popping, instead just replace at an index.

const arr = [2, 5, 7, 9];
arr[2] = 8; // 👉 O(1)
console.log(arr[2]); // 8

The position can be easily calculated by the index number and each item size.

Irrespective of the index position, time taken to insert a value is a constant.

Big-O of inserting an item to array is O(1)

--- ○ ---
Joby Joseph
Web Architect