Remove An Element From An Array In C

For instance, let's consider an array with seven elements, denoted as arr = {10, 25, 14, 8, 12, 15, 5); and the user intends to remove the element 8. To achieve this, the user initially needs to specify the index of the 8th element, which in this case is the 4th element. Subsequently, it is crucial to verify the feasibility of the deletion operation. It is imperative that the index of the element being targeted for deletion does not exceed the total number of elements in the array. In the given scenario, the array comprises 7 elements, and the user's attempt to erase the element at the 8th position is unattainable.

Steps to remove an element from an array

Here are the steps to eliminate a specific element from an array in C programming:

  1. Iterate through the array to find the target element.
  2. Once the element is located, shift all subsequent elements to the left to fill the gap.
  3. Update the array size to reflect the removal of the element.
  4. Ensure to handle boundary cases, such as removing the last element.
  5. Finally, the desired element is successfully removed from the array.

Step 1: Enter the array size using the variable num, then initialize the pos variable for position definition, and i for counting.

Use a loop to add the items into an array until (i reaches num + 1)); In this scenario, 5 is smaller than (num+1). Consequently, the condition is untrue, leading to the execution of the else block, which eliminates the element at the 5th position using a for loop. Finally, the modified array is displayed.

Example 2: Program to remove the specific character from an array using for loop

Let's explore an illustration on removing a specific element from an array at a given index using a for loop in the C programming language.

Example

/* program to remove the specific elements from an array in C. */
#include <stdio.h>
#include <conio.h>

int main ()
{
	// declaration of the int type variable
	int arr[50];
	int pos, i, num; // declare int type variable
	printf (" \n Enter the number of elements in an array: \n ");
	scanf (" %d", &num);
	
	printf (" \n Enter %d elements in array: \n ", num);
	
	// use for loop to insert elements one by one in array
	for (i = 0; i < num; i++ )
	{	printf (" arr[%d] = ", i);
		scanf (" %c", &arr[i]);
	}
	
	// enter the position of the element to be deleted
	printf( " Define the position of the array element where you want to delete: \n ");
	scanf (" %d", &pos);
	
	// check whether the deletion is possible or not
	if (pos >= num+1)
	{
		printf (" \n Deletion is not possible in the array.");
	}
	else
	{
		// use for loop to delete the element and update the index
		for (i = pos - 1; i < num -1; i++)
		{
			arr[i] = arr[i+1]; // assign arr[i+1] to arr[i]
		}
		
		printf (" \n The resultant array is: \n");
		
		// display the final array
		for (i = 0; i< num - 1; i++)
		{
			printf (" arr[%d] = ", i);
			printf (" %c \n", arr[i]);
		}
	}
	return 0;
}

Output:

Output

Enter the number of elements in an array:
8
Enter 8 elements in array:
arr[0] = a
arr[1] = b
arr[2] = f
arr[3] = h
arr[4] = e
arr[5] = k
arr[6] = w
arr[7] = p
Define the position of the array element where you want to delete:
4

The resultant array is:
arr[0] = a
arr[1] = b
arr[2] = f
arr[3] = e
arr[4] = k
arr[5] = w
arr[6] = p

Example 3: Program to delete a particular element from an array using the user-defined function

Let's explore an instance to showcase the removal of a specific element utilizing a custom function in the C programming language.

Example

/* program to delete a particular element from an array using user defined function in c. */
#include <stdio.h>
#include <conio.h>

// create user defined function update elements
addElement (int *arr, int num, int pos)
{
	int i;
	// use for loop to update the index
	for (i = pos-1; i < num - 1; i++)
	{
		arr[i] = arr[i+1]; // 
	}
}

// create disp() function to print the input array elements
disp( int arr[], int num)
{
	int i;
	// use for loop to print the resultant array
	for ( i = 0; i < num; i++)
	{
		printf( "\n arr[%d] = %d", i, arr[i]);
	}
}

int main ()
{
	// declaration of the array
	int arr[100];
	int i, num, pos, result;
	
	printf (" Enter the size of the array: ");
	scanf (" %d", &num);
	
	printf (" \n Enter the %d elements in an array: \n", num);
	// use for loop to input the elements one by one
	for (i = 0; i < num; i++)
	{
		printf (" arr[%d] = ", i);
		scanf (" %d", &arr[i]);
	}
	
	// define the position of the element to be deleted from an array
	printf (" Enter the position of the element you want to delete from an array: ");
	scanf (" %d", &pos);
	
// check whether thr defined pos is less than equal to size of array (num) and larger than 0
	if (pos <= num && pos > 0)
	{
		printf (" Array before deletion: \n");
		
		// call disp() function print the array
		disp (arr, num);
		// call addElement() function
		addElement(arr, num, pos);
		
		printf (" \n Array after deletion: \n");
		// disp() function to print the updated array
		disp( arr, num-1);
	}
	return 0;	
}

Output:

Output

Enter the size of the array: 8
Enter the 8 elements in an array:
arr[0] = 2
arr[1] = 4
arr[2] = 6
arr[3] = 8
arr[4] = 10
arr[5] = 3
arr[6] = 9
arr[7] = 12
Enter the position of the element you want to delete from an array: 4
Array before deletion:

arr[0] = 2
arr[1] = 4
arr[2] = 6
arr[3] = 8
arr[4] = 10
arr[5] = 3
arr[6] = 9
arr[7] = 12
Array after deletion:

arr[0] = 2
arr[1] = 4
arr[2] = 6
arr[3] = 10
arr[4] = 3
arr[5] = 9
arr[6] = 12

Example 4: Program to remove the specific element from an array using the pointers

Let's explore an instance of removing a specific element from an array using pointers and a custom function in the C programming language.

Example

/* create a program to delete a specific element from an array using pointers in C. */
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

// create a function to delete the specific element
void del (int num, int *arr, int index);

int main ()
{
	// declaration of the variables
	int *arr, num, i, index;
	printf (" Define the size of the array: ");
	scanf (" %d", &num);
	
	// use malloc function to dynamic create space for an array
	arr = (int *) malloc (sizeof(int) *num);
	
	printf (" \n Enter %d elements in an array: ", num);
	// use for loop to insert element one by one
	for (i = 0; i < num; i++)
	{
		printf (" \n arr[%d] = ", i);
		scanf (" %d", (arr + i));
	}
	
	// position of the element to be deleted
	printf (" Enter the position of the element to be deleted from an array: ");
	scanf (" %d", &index);
	del (num, arr, index); /* call delete function to delete the position of element and print the resultant array. */
	return 0;
}

// function definition
void del (int num, int *arr, int index)
{
	int i, j;
	// check position of the index
	if (index <= num)
	{
		// use for loop to delete the position of the defined index
		for (i = index - 1; i < num; i++)
		{
			j = i + 1; // assign the elments to j
			*(arr + i) = *(arr + j);
		}
		
		// display the resultant array after deletion of the specific element
		printf (" \n The resultant array after deletion of the array elements are: \n");
		for (i = 0; i < num - 1; i++)
		{
			printf (" arr[%d] = %d \n", i, ( *(arr + i)));
		}
	 } 
	 else
	 {
	 	printf (" \n Please enter correct position.");
	 }
}

Output:

Output

Define the size of the array: 8
Enter 8 elements in an array:
arr[0] = 5
arr[1] = 10
arr[2] = 15
arr[3] = 20
arr[4] = 25
arr[5] = 30
arr[6] = 35
arr[7] = 40
Enter the position of the element to be deleted from an array: 4
The resultant array after deletion of the array elements are:
arr[0] = 5
arr[1] = 10
arr[2] = 15
arr[3] = 25
arr[4] = 30
arr[5] = 35
arr[6] = 40

Input Required

This code uses input(). Please provide values below: