Deleting from an array
Deleting an element from an Array is a very common operation. It is also a very important operation because it is the basis for many other Array operations. In this article, we will discuss some interesting aspects of deleting an element from an Array.
Deleting an element from an Array can take many forms:
- Deleting an element from the end of the Array.
- Deleting an element from the beginning of the Array.
- Deleting an element from any given index inside the Array.
Deleting from the End of an Array
Deleting an element from the end of an Array is the most common operation. It is also the easiest operation to implement. All you need to do is check if the Array is empty, and if it is not, then you can delete the last element from the Array.
Here is the code for deleting an element from the end of an Array.
// Create a new array with a capacity of 6.
int[] array = new int[6];
// Current length is 0, because it has 0 elements.
int length = 0;
// Add 6 items into it.
for (int i = 0; i < 6; i++) {
array[i] = i * i;
// Each time we add an element, the length goes up by one.
length++;
}
// delete the last element from the Array.
if (length > 0) {
// Decrease the length by one.
length--;
}
Deleting from the Start of an Array
Next comes the costliest of all deletion operations for an Array—deleting the first element. If we want to delete the first element of the Array, that will create a vacant spot at the 0th index. To fill that spot, we will shift the element at index 1 one step to the left. Going by the ripple effect, every element all the way to the last one will be shifted one place to the left. This shift of elements takes time, where is the number of elements in the Array.
Here is the code for deleting an element from the start of an Array.
// Create a new array with a capacity of 6.
int[] array = new int[6];
// Current length is 0, because it has 0 elements.
int length = 0;
// Add 6 items into it.
for (int i = 0; i < 6; i++) {
array[i] = i * i;
// Each time we add an element, the length goes up by one.
length++;
}
// delete the first element from the Array.
if (length > 0) {
// Shift all the elements one step to the left.
for (int i = 0; i < length - 1; i++) {
array[i] = array[i + 1];
}
// Decrease the length by one.
length--;
}
Deleting from any given index inside the Array
Deleting an element from any given index inside the Array is the most complex operation. It is also the most expensive operation. If we want to delete an element from any given index inside the Array, that will create a vacant spot at that index. To fill that spot, we will shift the element at index + 1 one step to the left. Going by the ripple effect, every element all the way to the last one will be shifted one place to the left. This shift of elements takes time, where is the number of elements to be shifted.
Here is the code for deleting an element from any given index inside the Array.
// Create a new array with a capacity of 6.
int[] array = new int[6];
// Current length is 0, because it has 0 elements.
int length = 0;
// Add 6 items into it.
for (int i = 0; i < 6; i++) {
array[i] = i * i;
// Each time we add an element, the length goes up by one.
length++;
}
// delete the element at index 3 from the Array.
if (length > 0) {
// Shift all the elements one step to the left.
for (int i = 3; i < length - 1; i++) {
array[i] = array[i + 1];
}
// Decrease the length by one.
length--;
}