Insert / Delete

LESSON CONTENT

Insert / Delete

Insert Overview

Inserting an element into an array means adding a new value at a specific position. Because arrays use consecutive memory, making space for a new element often requires shifting other elements.

Insertion at the End

If the array has extra capacity, adding an element to the end is fast. The new value is placed directly after the last existing element, and no shifting is needed.

Insertion in the Middle

When inserting at an index somewhere in the middle, every element from that index onward must move one position to the right. This shifting keeps elements in order but increases the cost of insertion.

Why Insert Can Be Slow

Inserting early in a large array may require shifting many elements. The more elements that need to move, the slower the operation becomes.

Delete Overview

Deleting an element removes a value from a specific index. Because arrays must stay packed tightly in memory, removing an element creates a gap that must be filled by shifting the following elements.

Deleting at the End

Removing the last element is efficient. No shifting is required, and the array simply becomes one element shorter.

Deleting in the Middle

When deleting from the middle of an array, each element to the right must move one step left to fill the empty position and maintain consecutive memory.

Why Delete Can Be Slow

Like insertion, deletion becomes slower as the number of elements increases. If many elements must shift left, the deletion cost grows.