{
  "slug": "array-delete",
  "title": "Delete from an Array",
  "category": "Arrays",
  "difficulty": "easy",
  "complexity": {
    "time": "O(n)",
    "space": "O(1)"
  },
  "idea": "Removing position p leaves a hole; every element after p shifts one place left to close it. That shifting is the O(n) cost.",
  "code": [
    "delete(a, p):",
    "    remove a[p]",
    "    shift a[p+1..] left by one",
    "    length -= 1"
  ],
  "example": {
    "array": [
      4,
      8,
      15,
      16,
      23
    ],
    "at": 2
  },
  "invariant": "Each step is the real data structure at that step — the picture is a pure function of it.",
  "stepCount": 5,
  "steps": [
    {
      "i": 0,
      "op": "say",
      "caption": "Delete the element at index 2 — the value 15.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        4,
        8,
        15,
        16,
        23
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [],
      "regions": []
    },
    {
      "i": 1,
      "op": "read",
      "caption": "This is the one to remove.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        4,
        8,
        15,
        16,
        23
      ],
      "focus": [
        null,
        null,
        "read",
        null,
        null
      ],
      "markers": [
        {
          "name": "p",
          "index": 2,
          "tone": "pivot"
        }
      ],
      "regions": []
    },
    {
      "i": 2,
      "op": "remove",
      "caption": "Remove 15 — it leaves a hole at index 2.",
      "note": "15 leaves",
      "codeLine": 3,
      "stats": null,
      "array": [
        4,
        8,
        15,
        16,
        23
      ],
      "focus": [
        null,
        null,
        "remove",
        null,
        null
      ],
      "markers": [],
      "regions": []
    },
    {
      "i": 3,
      "op": "remove-close",
      "caption": "Everything after it flows left. The gap is closed; length is now 4.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        4,
        8,
        16,
        23
      ],
      "focus": [
        null,
        null,
        null,
        null
      ],
      "markers": [],
      "regions": []
    },
    {
      "i": 4,
      "op": "say",
      "caption": "Done — the array is contiguous again.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        4,
        8,
        16,
        23
      ],
      "focus": [
        null,
        null,
        null,
        null
      ],
      "markers": [],
      "regions": []
    }
  ]
}