C Program to Delete an Element in an Array
In this article, we will discuss how to delete an element in an array in C with their explanation. But before discussing a program, we must know about an array.
What is an Array?
An Array is a group of objects of the same data type that are kept together in a single memory block. In C programming, an array is derived data that holds values of fundamental data types like int, char, float, etc. An array is a fixed-size grouping of related data elements kept at adjacent memory regions. It can be used to store various user-defined and derived data types, including pointers and structures.
There are the following operations that can be performed on an Array:
- Insertion
- Deletion
- Sorting
- Searching
Here, we will discuss how to delete an element in an array in C programming.
Delete an element from an array in C:
Here, we will take a program that removes an element from an array by deleting it. The user will enter the location where the array element deletion is necessary. The removal of an element has no impact on the size of the array. It also determines whether deletion is possible or not. For instance, it is not possible to remove the sixth element from an array that contains five other elements.
- For instance, assume that a seven-element array, arr[] = 1, 8, 13, 5, 3, and 7, has one entry that the user wants to remove.
- Before determining whether or not the deletion is feasible, the user must first specify the position of the eighth element, which is located in the second position.
- A specific element's location shouldn't exceed the whole number of elements in an array.
- In this case, there are seven elements in the array, and the user wishes to remove the one in the eighth place, which is not possible.
Process to remove an element:
Some general procedures to remove an element from an array in C are as follows:
Locate the Index: First, find the element's index that you want to remove.
Check Validity: After that, verify the index's validity by making sure it is not possible and that it is not larger than or equal to the array size.
Move Elements: If the index is valid, move the elements after it in one position to the left to fill the void created by the deleted element. Use a loop to perform this shifting.
Update Size: If you are using a dynamically created array or a variable to track its size, decrease the size of the array by 1.
Example:
Let's take a program to delete an element from an array in C:
#include <stdio.h>
// Method to remove a component from an array
void deleteElement(int arr[], int *size, int indexToDelete) {
if (indexToDelete < 0 || indexToDelete >= *size) {
printf("Invalid index to delete\n");
return;
}
// Shift elements to the left to fill the gap
for (int i = indexToDelete; i < (*size) - 1; i++) {
arr[i] = arr[i + 1];
}
(*size)--;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int indexToDelete;
printf("Enter the index of the element to delete (0 to %d): ", size - 1);
scanf("%d", &indexToDelete);
deleteElement(arr, &size, indexToDelete);
printf("Array after deleting the element:\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Output:
![C Program to Delete an Element in an Array](https://static.tutorialandexample.com/c/c-program-to-delete-an-element-in-an-array1.png)
![C Program to Delete an Element in an Array](https://static.tutorialandexample.com/c/c-program-to-delete-an-element-in-an-array2.png)
Explanation:
In the above program, the steps to remove a specific element from an array are listed below:
- First, we take the deleteElement function. There are three requirements for this function:
void deleteElement(int arr[], int *size, int indexToDelete) {
It takes 3 parameters:
- arr[]: It represents the array of integers.
- size: It is a pointer to an integer containing the array's size.
- indexToDelete: It specifies the element's index that will be removed.
2. After that, the code determines whether the supplied index is within acceptable bounds. An error message is printed, and the function exits early if the index is less than 0 or larger than or equal to the array's size.
if (indexToDelete< 0 || indexToDelete>= *size) {
printf("Invalid index to delete\n");
return;
}
3. Next, the for loop traverses the array starting at the specified indexToDelete and continues until one position before the array's end. It replaces the element at the supplied index by shifting the components from one position to the left.
for (int i = indexToDelete; i< (*size) - 1; i++) {
arr[i] = arr[i + 1];
}
4. The array's size is reduced by 1 when the elements are shifted since one element was eliminated.
(*size)--;
5. In the main function, an integer array arr is initialized, and the variable size is determined by dividing the array's overall size by the size of a single element.
6. The program asks the user for the element's index, which stored in the indexToDelete variable.
7. The array, size, and indextodelete are passed to the deleteElement method when invoked to carry out the deletion. After the deletion, the elements still present in the modified array are then printed using a loop.