What Is Array Deletion?
Array deletion is the process of removing an element from an array. The element may be removed from the beginning, end, or a specific position in the array.
When an element is removed from the middle or beginning of an array, the elements that come after it generally need to be shifted toward the left to fill the empty position and preserve the order of the remaining elements.
Example of Array Deletion
Consider the following array:
[10, 20, 30, 40, 50]
If we delete the element at index 2, the value
30 is removed and the elements after it shift one position
toward the beginning.
Before:
[10, 20, 30, 40, 50]
Delete index 2:
[10, 20, 40, 50]
The value 40 moves from index 3 to index
2, and 50 moves from index 4 to
index 3.
Why Do Array Elements Shift After Deletion?
Arrays maintain elements in an ordered sequence of positions. When an element is removed, leaving a gap in the middle would break the normal contiguous representation of the remaining elements.
Therefore, the elements after the deleted position are moved one position to the left.
Delete 30:
[10, 20, 30, 40, 50]
↓
Move 40 left:
[10, 20, 40, 40, 50]
Move 50 left:
[10, 20, 40, 50, 50]
Reduce the logical length:
[10, 20, 40, 50]
Types of Array Deletion
Array deletion can be performed in several common ways depending on the location or information available about the element.
- Delete the first element.
- Delete the last element.
- Delete an element at a specific index.
- Delete an element by value.
- Delete multiple elements from a range.
Deleting the First Element
Removing the first element of an array requires the remaining elements to shift toward the beginning.
Before:
[10, 20, 30, 40]
Delete first element:
[20, 30, 40]
Since nearly every remaining element may need to move, deletion from the beginning is typically O(n).
Deleting the Last Element
Removing the last element is usually more efficient because no other elements need to be shifted.
Before:
[10, 20, 30, 40]
Delete last element:
[10, 20, 30]
In a dynamic array, removing the last element is typically O(1) when no additional resizing work is required.
Deleting From a Specific Index
To delete an element at a particular index, every element after that position is shifted one position to the left.
Array:
[10, 20, 30, 40, 50]
Delete index 2:
[10, 20, 40, 50]
The number of shifts depends on how close the deleted element is to the beginning of the array.
The Array Deletion Algorithm
A basic array deletion algorithm follows these steps:
- Validate the index that should be deleted.
- Shift all elements after that index one position to the left.
- Reduce the logical size of the array.
Unlike insertion, deletion shifts elements from left to right positions because each later element replaces the element before it.
Manual Array Deletion in JavaScript
JavaScript provides built-in methods for deleting elements, but implementing deletion manually helps demonstrate the underlying array operation.
function deleteAt(array, index) {
if (index < 0 || index >= array.length) {
throw new Error("Invalid deletion index");
}
for (let i = index; i < array.length - 1; i++) {
array[i] = array[i + 1];
}
array.length--;
}
const numbers = [10, 20, 30, 40, 50];
deleteAt(numbers, 2);
console.log(numbers);
// [10, 20, 40, 50]
Using splice() for Array Deletion
In practical JavaScript code, the splice() method can remove
one or more elements from an array.
const numbers = [10, 20, 30, 40, 50];
numbers.splice(2, 1);
console.log(numbers);
// [10, 20, 40, 50]
The first argument specifies the starting index and the second argument specifies the number of elements to remove.
Deleting the First Element With shift()
JavaScript provides shift() for removing the first element
from an array.
const numbers = [10, 20, 30, 40];
const removed = numbers.shift();
console.log(removed); // 10
console.log(numbers); // [20, 30, 40]
Because the remaining elements need to move toward the beginning, removing the first element is typically O(n).
Deleting the Last Element With pop()
The pop() method removes and returns the last element.
const numbers = [10, 20, 30, 40];
const removed = numbers.pop();
console.log(removed); // 40
console.log(numbers); // [10, 20, 30]
Since no remaining elements need to shift, removing the last element is typically O(1).
Deleting an Element by Value
Sometimes the application knows the value to remove rather than its index. In that case, the value must first be located and then removed.
function deleteByValue(array, value) {
const index = array.indexOf(value);
if (index !== -1) {
array.splice(index, 1);
}
}
const numbers = [10, 20, 30, 40];
deleteByValue(numbers, 30);
console.log(numbers);
// [10, 20, 40]
Finding the value with indexOf() can take O(n), and removing
an element from the middle can also require shifting elements. Therefore,
deleting by value is typically O(n).
Array Deletion Time Complexity
The time complexity of array deletion depends primarily on how many elements must be shifted.
| Deletion Operation | Typical Complexity | Reason |
|---|---|---|
| Delete first element | O(n) | Remaining elements shift left |
| Delete middle element | O(n) | Elements after the position shift |
| Delete last element | O(1) | No shifting is normally required |
| Delete by value | O(n) | Search plus possible shifting |
Best and Worst Cases
Deleting the last element is generally the best case because no elements need to be shifted. Deleting the first element is generally the most expensive because every remaining element may need to move.
| Case | Complexity |
|---|---|
| Delete last element | O(1) |
| Delete from middle | O(n) |
| Delete first element | O(n) |
Deleting Multiple Elements
An application may need to remove a range of consecutive elements. For example, removing indexes 2 through 4 from an array requires the remaining elements to close the resulting gap.
Before:
[10, 20, 30, 40, 50, 60]
Delete indexes 2 through 4:
[10, 20, 60]
In JavaScript, this can be performed using splice():
const numbers = [10, 20, 30, 40, 50, 60];
numbers.splice(2, 3);
console.log(numbers);
// [10, 20, 60]
Array Deletion vs Array Update
Deletion and updating are different operations. Updating replaces an existing value while keeping the array length unchanged. Deletion removes an element and reduces the number of elements.
Update:
[10, 20, 30]
↓
[10, 25, 30]
Deletion:
[10, 20, 30]
↓
[10, 30]
Array Deletion vs Linked List Deletion
Arrays and linked lists have different performance characteristics for deletion. Arrays may need to shift elements after a deletion, while a linked list can remove a node by changing references once the target position has been located.
| Operation | Array | Linked List |
|---|---|---|
| Delete first element | O(n) | O(1) with a head reference |
| Delete at known position | O(n) | O(1) after reaching the node |
| Random access | O(1) | O(n) |
This is an important data-structure trade-off. Arrays provide efficient indexed access, while linked lists can avoid large-scale element shifting during structural insertion and deletion.
Validating the Deletion Index
Before deleting by index, the requested position should be checked to make sure it refers to an existing element.
For:
[10, 20, 30]
Valid indexes:
0, 1, 2
Invalid:
-1
3
4
Common Array Deletion Mistakes
- Deleting an invalid index.
- Forgetting to reduce the logical size after manual deletion.
- Shifting elements in the wrong direction.
- Confusing deletion with updating an element.
- Assuming deletion from every position takes O(1).
- Ignoring the search cost when deleting by value.
- Using repeated beginning deletions when another data structure would be more suitable.
When Is Array Deletion Efficient?
Array deletion is most efficient when removing the last element because existing elements do not need to move. Deleting from the beginning or middle becomes more expensive as the number of elements that must shift increases.
If an application frequently removes elements from the beginning or performs many structural deletions in the middle of a large collection, it may be worth considering another data structure.
Key Takeaways
- Array deletion removes an element from a specific position or by value.
- Deleting from the beginning or middle usually requires shifting elements.
- Elements are shifted from left to right positions after a deletion.
- Deleting the last element is typically O(1).
- Deleting from the beginning or middle is typically O(n).
- Deleting by value usually requires searching first, making it O(n).
- JavaScript provides
splice(),shift(), andpop()for common deletion operations. - Arrays provide fast indexed access but can be expensive for frequent structural deletions.