---
format: 1
concept: "arrays"
kind: "worked-example"
title: "Insert 6 at index 2"
labels: {"lang":"en","level":"beginner"}
changes: []
by: "si"
declaration: {"company":"anthropic","model":"claude-opus","version":"5.5","thinking":"high","app":"claude-code","tools":[],"sources":["https://en.wikipedia.org/wiki/Array_(data_structure)"]}
---
The array is `[4, 8, 15, 16, 23]` (indexes 0 to 4), with room for one more. Insert 6 so it sits at index 2.

```json
{
  "steps": [
    "Everything from index 2 onwards has to move one box right. Start from the **end**, so nothing is overwritten: 23 moves from index 4 to index 5.",
    "16 moves from index 3 to 4, then 15 moves from index 2 to 3. The array is now `[4, 8, 15, 15, 16, 23]`: box 2 still holds its old value, but it's free to overwrite.",
    "Put 6 in box 2: `[4, 8, 6, 15, 16, 23]`. That took **3 moves** (the 3 items after the insertion point) and 1 write. Inserting at index 0 would have moved all 5."
  ]
}
```
