In this article we will go through different scenarios for deleting an element at a given index in an array data structure. For all the scenarios explained below, we take example of an array which has enough memory to store a maximum of 7 elements. But only 5 elements are stored and a variable usedLength
is used to store the actual number of elements stored in the array.
myArray = [1, 2, 3, 4, 5, _, _]
usedLength = 5
Note that the reason for this article is just to explain how delete operation is done for arrays at a low level. Many programming languages have high level functions/methods which allows you to directly delete an element with a function call. For example in python you can do something like array.pop(position)
.
Deletion at the end of Array
In this scenario, the goal is to delete the last element stored in the array. This is the simplest of all scenarios as movement of other elements present in the array is not required. We simply overwrite the last element to an empty value and reduce the count of elements stored in the array.
#include <stdio.h>
int main() {
// An array which can store 7 elements but only 5 are used
int myArray[] = {1, 2, 3, 4, 5, 0, 0};
// Store the number elements present in the array
int usedElements = 5;
if (usedElements > 0) {
// Set the last element to 0 to indicate empty space
myArray[usedElements] = 0;
// Decrement the count of used elements to delete the last element
usedElements--;
printf("Element at the end has been deleted.\n");
// Print the updated array
printf("Updated Array: ");
for (int i = 0; i < usedElements; i++) {
printf("%d, ", myArray[i]);
}
printf("\n");
} else {
printf("Array is already empty.\n");
}
return 0;
}
Deletion at the start of Array
In this scenario, the goal is to delete the first element in the array. Since all the elements stored in the array are present in continuous memory locations, there should not be any gap at the beginning or anywhere in the array after deletion. So we can’t just overwrite the first element to some empty value and continue.
We can perform this operation by making the first element empty and then shifting all elements starting from index 1
to one position to the left.
Below is an example illustration on how this process looks in theory:
[1, 2, 3, 4, 5, _, _]
// Delete the first element:
[_, 2, 3, 4, 5, _, _]
// Starting from index 1,
// Shift all elements one position left:
[2, _, 3, 4, 5, _, _]
[2, 3, _, 4, 5, _, _]
[2, 3, 4, _, 5, _, _]
[2, 3, 4, 5, _, _, _]
Programmatically, we just overwrite the previous index value with the current index value. So this is how it actually looks:
// Starting from index 1
// Move all elements one position left
[1, 2, 3, 4, 5, _, _]
[2, 2, 3, 4, 5, _, _]
[2, 3, 3, 4, 5, _, _]
[2, 3, 4, 4, 5, _, _]
[2, 3, 4, 5, 5, _, _]
// Now mark index 4 as empty
[2, 3, 4, 5, _, _, _]
Below is the code for deleting an element at the start of an array:
#include <stdio.h>
int main() {
// An array which can store 7 elements but only 5 are used
int myArray[] = {1, 2, 3, 4, 5, 0, 0};
// Store the number elements present in the array
int usedElements = 5;
if (usedElements > 0) {
// Starting from index 1, Shift all elements one position to the left
for (int i = 1; i < usedElements; i++) {
myArray[i-1] = myArray[i];
}
myArray[usedElements - 1] = 0; // Set the last element to 0 to indicate empty space
usedElements--; // Decrement the count of used elements
// The memory of the entire array now looks like this
// [2, 3, 4, 5, 0, 0, 0]
printf("Element at the start has been deleted.\n");
// Print the updated array
printf("Updated Array: ");
for (int i = 0; i < usedElements; i++) {
printf("%d, ", myArray[i]);
}
printf("\n");
} else {
printf("Array is already empty.\n");
}
return 0;
}
Deletion at a given position in the Array
In this scenario, the goal is to delete an element at any given index in the array. All the existing elements to the right of delete index needs to be moved one position to the left. This operation moves the vacancy created at delete index to the ending of array.
Below is an example illustration on how this process looks for deleting an new element at index 2
:
[1, 2, 3, 4, 5, _, _]
// Delete element at index 2:
[1, 2, _, 4, 5, _, _]
// Starting from index 3
// Shift all elements one position left
[1, 2, 4, _, 5, _, _]
[1, 2, 4, 5, _, _, _]
Below is the code for inserting an element at a given position in the array:
#include <stdio.h>
int main() {
int myArray[] = {1, 2, 3, 4, 5, 0, 0};
// Number of elements currently in the array:
int usedElements = 5;
// Index of the element to delete (0-based index):
int indexToDelete = 2;
// Check for validity of given index and then delete
if (indexToDelete >= 0 && indexToDelete < usedElements) {
// Starting from index to delete, Shift all elements one position to the left
for (int i = indexToDelete+1; i < usedElements; i++) {
myArray[i-1] = myArray[i];
}
// Set the last element to 0 to indicate empty space
myArray[usedElements - 1] = 0;
// Decrement the count of used elements
usedElements--;
printf("Element at index %d has been deleted.\n", indexToDelete);
// Print the updated array
printf("Updated Array: ");
for (int i = 0; i < usedElements; i++) {
printf("%d, ", myArray[i]);
}
printf("\n");
} else {
printf("Invalid index. Element cannot be deleted.\n");
}
return 0;
}