Skip to main content
Beginner ~12 min read

Arrays: Traversal, Insertion & Deletion

The fundamental data structure: indexed access, traversal, and why inserting in the middle shifts everything.

Prerequisites: What are Data Structures & Algorithms?

An array stores elements in one contiguous block of memory, where each element sits at a computable address: element i lives at "start + i × element size". That single design decision explains everything about arrays. Random access, "give me element 7", is O(1), because the computer just does the arithmetic and jumps there. No other common structure answers random index questions faster.

The same design makes structural changes expensive. Insert an element in the middle and every element after it must shift one slot right to make room, O(n). Delete one and everything after shifts left to close the gap, O(n). Append at the end is usually O(1) amortized (JavaScript engines over-allocate capacity), which is why "push then process" patterns are so common.

Array operations at a glance

Operation / caseCostNotes
Access by index arr[i]O(1)the array superpower
Search (unsorted)O(n)must check each element
Search (sorted)O(log n)binary search, see Module 3
Insert / delete at endO(1) amortizedpush() / pop()
Insert / delete at front or middleO(n)elements must shift

Traversal and in-place edits

array-basics.js
const scores = [72, 85, 60, 91, 78];

// Traversal, the workhorse of array algorithms, O(n)
let total = 0;
for (let i = 0; i < scores.length; i++) {
  total += scores[i];
}

// Insert at index 2: shift everything from index 2 onward right
scores.splice(2, 0, 88); // [72, 85, 88, 60, 91, 78], O(n) behind the scenes

// Delete at index 0: shift everything left
scores.shift(); // [85, 88, 60, 91, 78], O(n)

// Append at end: O(1) amortized
scores.push(95);

Strings are arrays of characters (almost)

Strings behave like read-only arrays of characters: s[i] is O(1), s.length is O(1), and scanning a string is the same O(n) walk. The crucial difference is immutability, strings cannot be edited in place. Building a string with += inside a loop can copy the whole string each time, degrading to O(n²). Collect pieces in an array and join once at the end instead.

Many classic problems, palindromes, anagrams, deduplication, are array techniques wearing a string costume. When a string problem feels awkward, convert it to an array of characters (s.split('')), solve it with array tools, and join back.

Practice

You repeatedly need to remove the first item of a large array (like a queue). What is the total cost of doing this n times, and what structure fixes it?

Reveal explanation

Each front-removal is O(n) because all n-1 remaining elements shift left. Doing it n times costs O(n²). A queue built for FIFO access dequeues in O(1).

Check your understanding

  1. 1.Why is arr[7] an O(1) operation?
  2. 2.Which operation is NOT O(1) on a typical dynamic array?
  3. 3.You're building a 100,000-character string one character at a time in a loop. What's the smarter approach?

Finished reading?

Mark it complete to earn +50 XP. Progress stays in your browser.

Visualize it

Linear Search

Play, pause, and scrub the real algorithm step by step.

Keep going