SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Course Overview
Arrays, Strings & Hash Maps OverviewArraysStringsHash Maps and SetsArrays, Strings & Hash Maps Quiz
Built-in Data Structures

Arrays

Learn how array indexing works and how to traverse, insert, and delete elements in JavaScript and Python.

An array keeps values in an ordered sequence. Each value is an element, and its numbered position is an index. Arrays are useful when you need to keep a collection in order and read an element by its position.

Values:   ["red", "green", "blue"]
Indexes:     0       1        2

Indexes start at 0, so the first element is at index 0 and the last element of a collection with n elements is at index n - 1.

How Arrays Store and Access Values

Index 2 points directly to C, while searching for D scans the sequence from A.

A traditional fixed-size array reserves adjacent memory slots for elements of one type. Knowing the starting address and an index lets a program locate an element directly.

JavaScript arrays and Python lists are the growable collections used in this lesson. They support the same indexed operations, but they can also grow, shrink, and contain values of different types. Their implementations manage storage for you; do not assume the values themselves occupy one continuous block of memory.

const colors = ["red", "green", "blue"];
console.log(colors[1]); // "green"

Big O: Checking by Index

Reading colors[1] is O(1) time: the collection can go directly to that index, so the work does not grow with the number of elements. If you know only a value and need to find its index, an unsorted collection may require checking all n elements. That search is O(n) time in the worst case.

Traversing an Array

Traversal means visiting elements one at a time. Use an index-based loop when you need each position, or a value-based loop when you only need the elements.

When the Index Matters

This loop prints each position alongside its value:

const colors = ["red", "green", "blue"];

for (let index = 0; index < colors.length; index++) {
  console.log(index, colors[index]);
}
// 0 red
// 1 green
// 2 blue

When Only the Values Matter

If the positions are irrelevant, iterate over the elements directly:

const colors = ["red", "green", "blue"];

for (const color of colors) {
  console.log(color);
}
// red
// green
// blue

Big O: Traversal

Both loops visit each of the n elements once, so a complete traversal takes O(n) time. In the indexed loop, each colors[index] read is O(1), but doing that read n times still totals O(n). These examples keep only a loop variable, so they use O(1) extra space.


Inserting Elements

Appending X leaves A, B and C in place. Inserting X at index 1 shifts B and C right.

Insertion adds an element at a chosen index. Adding at the end usually avoids moving existing elements. Adding at the beginning or middle shifts later elements one position to the right to make room.

Insert atBeforeAfterElements shifted
Beginning["B", "C"]["A", "B", "C"]Every existing element
End["A", "B"]["A", "B", "C"]None
Index 2["A", "B", "D"]["A", "B", "C", "D"]"D"

The examples below start from a fresh collection in each case.

Insert at the Beginning

const letters = ["B", "C"];
letters.unshift("A");
console.log(letters); // ["A", "B", "C"]

Moving all existing elements makes beginning insertion O(n).

Insert at the End

const letters = ["A", "B"];
letters.push("C");
console.log(letters); // ["A", "B", "C"]

Suppose a dynamic array has four slots, all occupied by A, B, C, and D. In a model that doubles capacity, appending E copies four elements into eight slots and writes E into the fifth. The next three appends fit in spare slots.

A full four-slot array grows to eight slots. A through D are copied and E occupies the fifth slot.

One resize takes O(n) work. Across a series of appends, capacity grows geometrically, so the total copying is linear in the number of appends. The average cost over that sequence is constant. This is amortized O(1). Different runtimes choose different growth factors.

Insert at a Specific Index

To put "C" at index 2, the existing "D" moves one position to the right:

const letters = ["A", "B", "D"];
letters.splice(2, 0, "C");
console.log(letters); // ["A", "B", "C", "D"]

Middle insertion can shift many elements, so it takes O(n) time.


Deleting Elements

Removing B gives A, C, D when order matters and A, D, C when the last value replaces B.

Deletion removes an element at a known index. Choose the technique based on whether the remaining elements must keep their order.

Preserve the Order

Removing "B" at index 1 shifts "C" and "D" left to close the gap:

const letters = ["A", "B", "C", "D"];
letters.splice(1, 1);
console.log(letters); // ["A", "C", "D"]

An ordered deletion from the beginning or middle takes O(n) because later elements shift. Removing the last element needs no shift and takes O(1).

When Order Does Not Matter

If order is irrelevant, replace the element at the target index with the last element, then remove the last slot:

Start:                 ["A", "B", "C", "D"]
Replace index 1:       ["A", "D", "C", "D"]
Remove the last slot:  ["A", "D", "C"]
const letters = ["A", "B", "C", "D"];
const index = 1;

letters[index] = letters[letters.length - 1];
letters.pop();
console.log(letters); // ["A", "D", "C"]

With a valid index already known, this takes O(1) time. If you must search for the value first, finding its index can still take O(n).

Choose based on order

Use ordered deletion when positions matter. Use replacement with the last element only when changing the order is acceptable.

Big O Notation: Operation Summary

Here n is the number of elements in the collection.

OperationTimeWhy
Access by known indexO(1)Go directly to that position.
Traverse the arrayO(n)Visit all n elements.
Search by valueO(n) worst caseCheck up to n elements.
Insert at endAmortized O(1)Usually add without shifting; occasional growth copies elements.
Insert at beginning or middleO(n)Shift later elements right.
Delete last elementO(1)No other elements move.
Delete while preserving orderO(n)Shift later elements left.
Delete without preserving orderO(1)Replace with the last element, then remove it.

Key Takeaways

  • Arrays keep elements in order and provide O(1) access when you know an index.
  • Use index-based traversal when positions matter; otherwise, iterate over values directly. Either way, visiting every element takes O(n).
  • Appending is usually fast. Inserting at the beginning or middle shifts elements and takes O(n).
  • Ordered deletion shifts elements. If order does not matter and the index is known, replacing the element with the last one avoids that shift.
  • JavaScript arrays and Python lists are growable indexed collections; a traditional fixed-size array has stricter storage and type rules.

How is this lesson?

Previous lessonArrays, Strings & Hash Maps OverviewNext lessonStrings
Edit on GitHubReport on GitHub
On this page
%

On this page

How Arrays Store and Access ValuesBig O: Checking by IndexTraversing an ArrayWhen the Index MattersWhen Only the Values MatterBig O: TraversalInserting ElementsInsert at the BeginningInsert at the EndInsert at a Specific IndexDeleting ElementsPreserve the OrderWhen Order Does Not MatterBig O Notation: Operation SummaryKey Takeaways