Browse curriculum

Array Operations

Array operations are the four basic ways to work with an array: insert, delete, search and update.

5 / 10 ELEMENTS

READY

10

0

20

1

30

2

40

3

50

4

 

Insert, delete, access or search — and watch which of them have to move the other elements.

 

Value

Index

Intuition

Arrays are contiguous memory blocks. Accessing an element by index is instant (O(1)), but inserting or deleting elements requires shifting subsequent elements, which can be slow (O(n)).

Concept

  • Access (Get/Update): O(1) - Direct memory address calculation.
  • Search: O(n) - Linear scan (unless sorted).
  • Insertion: O(n) - Requires shifting elements to make space.
  • Deletion: O(n) - Requires shifting elements to fill the gap.

How it Works

Insertion at Index i:

  1. Shift elements from index i to end one position to the right.
  2. Place the new value at index i.

Deletion at Index i:

  1. Shift elements from index i+1 to end one position to the left.
  2. Decrease array size.

Step-by-Step Breakdown

Watch the visualization:

  • Notice how elements move during Insert/Delete.
  • Observe the immediate highlight for Update.
  • See the linear scan for Search.

Trace: A = [10, 20, 30, 40, 50]

get(2)

One address calculation: the base plus two element widths. No element is examined and nothing moves, whatever the length of the array.

30 · 0 moves

search(40)

Compares 10, 20, 30, then 40. A value carries no information about where it lives, so on unsorted data a scan is the only option — sorting first would buy a binary search.

index 3 · 4 reads

insert(1, 15)

A gap has to open at index 1, so 50, 40, 30 and 20 each move one slot right, in that order — right to left, or they would overwrite each other. Then 15 is written into the hole.

4 moves

state

Six elements now, and the array had to have room for the sixth. A fixed-size array would have needed a reallocation and a full copy here.

[10, 15, 20, 30, 40, 50]

delete(0)

Removing the first element leaves a hole at the front, so all five survivors move one slot left. This is the worst case for deletion: every remaining element moves.

5 moves

state

Back to five elements.

[15, 20, 30, 40, 50]

the pattern

Moves at the front are N, moves at the back are 0. Push and pop at the end are effectively O(1), which is why a stack on an array is a good fit and a queue on one is not — a queue takes from the front, so every dequeue would shift the whole array. A circular buffer fixes that by moving the index instead of the data.

O(N) at the front

When to Use

  • Fast access by index is required.
  • Size is known or fixed (static arrays).
  • Simple data storage.

When NOT to Use

  • Frequent insertions/deletions in the middle (use Linked List).
  • Unknown size (use Dynamic Array / Vector).

How to Identify

Problems involving sequences, indices, or fixed-size collections.

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description

Frequently Asked Questions

Why is access O(1) but search O(N)?

An index becomes an address by one multiply and add, because the elements are contiguous. A value carries no clue about its position, so finding one needs a scan, unless the array is sorted and you can binary search.


Why do insertion and deletion cost O(N)?

Contiguous storage means making room at position i shifts everything after it one slot right, and removing it shifts them back left. Only inserting or deleting at the end avoids the shift.


When is a linked list the better choice?

When you splice in the middle far more often than you index, since a linked list inserts in O(1) once you hold the node. You give up O(1) random access and cache locality to get it.