02 / 19

Why is insertion/deletion in the middle of an array O(n)?

Array Insertion and Deletion Complexity

javascript
  1. 1

    Insertion may require shifting up to n elements.

  2. 2

    Deletion may require shifting up to n elements toward the left.

  3. 3

    Insertion or deletion at the end can be O(1) when capacity is available.

  4. 4

    Insertion or deletion at the beginning is O(n) because almost every element may move.

Difficulty: 2/10

Follow-up Questions

  • When can array insertion be O(1)?
  • How does a linked list behave differently?