In this tutorial, we are going to learn to write C Program to delete a given element from an array. To delete the given element we will compare the given element with the array elements. If we find a match then will delete the element from the array otherwise will check for the next array element. This process will continue till the end of the array.
Our problem statement
Suppose we have an array that has elements a={1,2,3,4,5}. We want to delete element 3 from the array. So after deleting element 3 from the array, the new array would be a={1,2,4,5}.
Here we are deleting the element from an array, not deleting the elements from the array index.
Our approach to to delete given element from array
- We will take the value in the array
- Now we will compare the element which we want to delete from array with all array elements.
- To iterate array we are using for loop in c and to compare elements we are using equal(==) operator and if statement.
- We have also handled the scenario, suppose if we have element that is repeated multiple times. We will delete the all occurrence as well.
C Program to delete given element from array
#include <stdio.h> #include <stdlib.h> int main() { int i, size, value,j,temp; printf("C Program to delete given element from array\n"); printf("Enter size of the array : "); scanf("%d", &size); int arr[size]; temp=size; for(i = 0; i < size; i++) { printf("Please give value for index %d : ",i); scanf("%d",&arr[i]); } printf("Enter the element to delete : "); scanf("%d", &value); for(i=0; i<size; i++) { if(arr[i]==value){ for(j=i; j<size-1; j++){ arr[j] = arr[j+1]; } size--; i--; ; } } if(temp==size){ printf("No element %d found in array ",value); exit(0); } printf("Rest elements of array after deleting %d are\n",value); for(i=0; i<size; i++) { printf("%d\n", arr[i]); } return 0; }
Output
[wpusb]
Also Prepare Below Important Question
- Sum of digits of Given Number in Java
- Hibernate Interview Questions for 2+ years of experience
- 68 Most Important Microservices Interview Questions
- 60 Most Important Git Interview Questions
- 50+ Mostly asked Java Interview Questions for 6 Years Exp.
- 60+ Mostly Asked Java Interview Questions for 4 Years Exp
- 60+ Mostly asked Java Interview Question for 3+ Years
- 60+ Spring Boot interview questions for 4+ years Exp.
- 60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
- Scenario Based Java 8 Coding Interview Questions (For Experienced)
- Python Program to add two numbers without addition operator
- Mostly Asked Java Interview Questions For 2 Yrs Experience
- Find All Pairs in Array whose Sum is Equal to given number Using Java
- Java Program to find GCD of two Numbers using Recursion
- Python Program to Separate Characters in a Given String
- Python Program to add two number using Recursion
- Python Program to Find Highest Frequency Element in Array
- Python Program to Merge two Arrays
- Perform left rotation by two positions in Array Using Python
- Python Program to Delete Element at Given Index in Array
Interview Questions Categories
C Programming Interview Preparation
Core Java Programming Interview Preparation
- Core Java Programming Coding Questions
- Core Java Pattern Programming Questions
- Core Java Programming Interview Questions