Arrays

Ordered indexable collection for O(1) access by position.

What is an array?

An array is an ordered collection of values stored in contiguous slots. Each slot has an index starting at 0, so you can read or write any position directly — arr[i] — without walking past everything before it.

Index
The address of a slot, counting from 0
Element
The value stored at that index
Contiguous
Slots sit next to each other in memory, in order

See it as slots

Picture a row of numbered mailboxes. Index 0 is the first box, 1 the next, and so on. Because the boxes sit in a straight line, knowing the index is enough — you jump straight to that box.

Watch the pointer move. Sequential steps walk neighbor to neighbor. Then it jumps to a later index in one hop — the same cost as opening the first box.

Six slots · index is the address
Readingarr[0] = 7

Same cost for any index. Contiguous slots make that possible.

Types of arrays

Not every array looks the same. In interviews and real systems you usually classify arrays in two ways: how their size is managed, and how many dimensions they use.

Static arrays keep a fixed length. Dynamic arrays can grow. One-dimensional arrays are a single row of values. Multi-dimensional arrays nest rows into tables or deeper grids.

How arrays are classified
FocusArray

Size answers “can it grow?” Shape answers “how is it laid out?”

How it is stored in memory

In TypeScript, when declaring an array creates a pointer in stack memory and that pointer is an address to heap memory where actual array values are stored.

Declare → allocate → index
StatusPress Next to declare arr

Start here. Each step highlights the TypeScript below.

Step 1 of 4

Operations

Think of the array as a row of numbered boxes. Here are the everyday things you can do with those boxes — tap Next on each demo to watch it happen. For every built-in method and copy-paste snippets, open the Functions tab.

Access

Peek inside one box by its number. You do not walk past the others — you open that box directly.

Look in one box
Statuslook up

Open box 0 — you go straight there.

Step 1 of 2

Update

Swap what sits in a box. Pick the number, put the new value in, leave the rest alone.

Change one box
Statuschange

Box 1 currently holds 3.

Step 1 of 2

Looking for a value? Start at the first box and walk along until you find it — or reach the end empty-handed.

Find a value
Statussearch

Looking for 9. Box 0 has 7 — keep walking.

Step 1 of 3

Insert at end

Add a new box at the end of the row. Nobody else has to move over.

Add at the end
Statusadd end

Four boxes are full. Add one more at the end.

Step 1 of 2

Insert at index

Squeeze a new value into the middle. Everyone after that spot slides one box to the right to make room.

Squeeze into the middle
Statusinsert

Want 5 in box 1. Everyone after must scoot right.

Step 1 of 3

Delete at end

Take away the last box. The rest of the row stays put.

Remove the last box
Statusremove end

Take away the last box only.

Step 1 of 2

Delete at index

Pull a box out of the middle. Everyone after it slides left so there is no empty hole in the row.

Remove from the middle
Statusremove

Remove box 1 (the 3). Close the gap.

Step 1 of 3

Traverse

Walk the whole row from first box to last. Useful when you need to see or use every value once.

Walk every box
Statuswalk

Visit box 0.

Step 1 of 4