Javatpoint Logo
Javatpoint Logo

Remove an element from an array in C

In this topic, we will learn how to delete or remove a particular element from an array in the C programming language. An array is the collection of the same data type elements or items stored in a contiguous memory block. In C programming, an array is derived data that stores primitive data type values like int, char, float, etc.

To delete a specific element from an array, a user must define the position from which the array's element should be removed. The deletion of the element does not affect the size of an array. Furthermore, we should also check whether the deletion is possible or not in an array.

Remove an element from an array in C

For example, suppose an array contains seven elements, arr[] = {10, 25, 14, 8, 12, 15, 5); and the user want to delete element 8. So, first, the user must define the position of the 8th element, which is the 4th, and then check whether the deletion is possible or not. The position of the particular element should not be more than the total elements of an array. Here, we have 7 elements in an array, and the user wants to delete the 8th position element, which is impossible.

Steps to remove an element from an array

Following is the steps to remove a particular element from an array in C programming.

Step 1: Input the size of the array arr[] using num, and then declare the pos variable to define the position, and i represent the counter value.

Step 2: Use a loop to insert the elements in an array until (i < num) is satisfied.

Step 3: Now, input the position of the particular element that the user or programmer wants to delete from an array.

Step 4: Compare the position of an element (pos) from the total no. of elements (num+1). If the pos is greater than the num+1, the deletion of the element is not possible and jump to step 7.

Step 5: Else removes the particular element and shift the rest elements' position to the left side in an array.

Step 6: Display the resultant array after deletion or removal of the element from an array.

Step 7: Terminate or exit from the program.

Example 1: Program to remove an element from an array using for loop

Let's create a program to delete an element from an array using for loop in the C programming language.

Output:

Enter the number of elements in an array:
8

Enter 8 elements in array:
arr[0] = 3
arr[1] = 6
arr[2] = 2
arr[3] = 15
arr[4] = 10
arr[5] = 5
arr[6] = 8
arr[7] = 12
Define the position of the array element where you want to delete:
5

The resultant array is:
arr[0] = 3
arr[1] = 6
arr[2] = 2
arr[3] = 15
arr[4] = 5
arr[5] = 8
arr[6] = 12

In the above program, we input 8 elements for an array arr[] from the user. After that, the user enters the position of the particular element is 5. And then, we checked the defined position by the If statement (if (pos > num + 1)); Here, 5 is less than the (num+1). So the condition is false, and it executes the else block to remove the 5th position of the element is 10 using for loop and then prints the resultant array.

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

Let's consider an example to delete the particular position of the defined character from an array using for loop in the C programming language.

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 consider an example to demonstrate the deletion of the particular element using the user defined function in the C programming language.

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 consider an example of deleting the particular element from an array using the pointers and the user defined function in the C programming language.

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

Next TopicSquare Root in C





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA