Remove one specific element from an array in C:

In this C programming tutorial, we will learn how to remove one specific element from an array in C. The algorithm of the program will be like below :

1. Ask the user to enter the total number of elements to add.
2. Take all inputs from the user and insert it in an array.
3. Ask the user to enter the element that needs to be removed.
4. Check if the element exist in the array or not.
5. If it does,move the array to one step left starting from the element position.
6. Reallocate the memory of the array and decrease the size by 1.
7. Print out the array.

So, if our array is [1,2,3,4,5], and if we want to remove 3 from this array, we will move the subarray starting from 4 to one step left and mark the last element as -1. The final array will be [1,2,4,5].

C program :

```#include <stdio.h>
#include <stdlib.h>
int main()
{
//1
int *array, size, elementToDelete, i, position;
//2
printf("Enter total number of elements to add : ");
scanf("%d", &size);
array = (int *)malloc(size * sizeof(int));
//3
for (i = 0; i < size; i++)
{
printf("Enter element for position %d : ", i);
scanf("%d", &array[i]);
}
//4
printf("You have entered : ");
for (i = 0; i < size; i++)
{
printf("%d ", array[i]);
}
printf("\n");
//5
printf("Enter the number you want to delete : ");
scanf("%d", &elementToDelete);
//6
position = -1;
//7
for (i = 0; i < size; i++)
{
if (array[i] == elementToDelete)
{
position = i;
break;
}
}
//8
if (position != -1)
{
//9
for (i = position; i < size - 1; i++)
{
array[i] = array[i + 1];
}
array = (int *)realloc(array, (size - 1) * sizeof(int));
//10
printf("Final array :");
for (i = 0; i < size - 1; i++)
{
printf("%d ", array[i]);
}
printf("\n");
}
else
{
//11
}
}```

Explanation :

The commented numbers in the above program denote the step numbers below :

1. Create one integer pointer variable array to hold int elements, size to hold the size of the array,elementToDelete to store the element to delete,i to use in loop and position to hold the position of the user provided element.
2. Ask the user to enter the total number of elements to add. Store the value in variable size. Using malloc, allocate the memory for array  to hold the size number of integers.
3. Using a for loop, Ask the user to enter the value for the array repeatedly. Read each value and store it in the array.
4. Print out the array to the user.
5. Ask the user to enter the number to delete. Read and store it in the elementToDelete variable.
6. Set the value of position as -1.
7. Check each value of the array one by one if any element is equal to elementToDelete. If yes, save its index position in the variable position.
8. Check the value of the variable position. If its value is not equal to -1, it means that the user provided element is available in the array.
9. Now, we need to move all elements of the array to one position left starting from the user input number position. We are also reallocating the array using the realloc method. The size is reduced by 1.
10. Print out the final array after the modification.
11. This printf statement will be used if the value of position is not -1 i.e. if the user input number is not found in the array.

Sample Output :

```Enter total number of elements to add : 5
Enter element for position 0 : 1
Enter element for position 1 : 2
Enter element for position 2 : 3
Enter element for position 3 : 4
Enter element for position 4 : 5
You have entered : 1 2 3 4 5
Enter the number you want to delete : 3
Final array :1 2 4 5

Enter total number of elements to add : 6
Enter element for position 0 : 2
Enter element for position 1 : 4
Enter element for position 2 : 6
Enter element for position 3 : 8
Enter element for position 4 : 9
Enter element for position 5 : 10
You have entered : 2 4 6 8 9 10
Enter the number you want to delete : 10
Final array :2 4 6 8 9

Enter total number of elements to add : 4
Enter element for position 0 : 2
Enter element for position 1 : 88
Enter element for position 2 : 23
Enter element for position 3 : 29
You have entered : 2 88 23 29
Enter the number you want to delete : 2
Final array :88 23 29
```

The above example is available on Github

Conclusion :

We have learned how to remove an array element using its value in C. Try to run the example above and drop one comment below if you have any queries.

Categories: c