Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
1.1.
Delete an element from the Array-based on position
1.2.
Delete an element from the Array-based on value.
1.3.
Delete an element from the Array by user-defined functions.
2.
FAQs
3.
Key Takeaways
Last Updated: Mar 27, 2024

Delete an element from the Array

Author Gaurav Joshi
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

This article will discuss how to delete an element from the Array in the c language. There are different ways in C language through which a user could delete an element from the Array or remove a particular element in an array. Before going ahead, let us understand what an array is? An array is a collection of similar data type elements stored in a contiguous memory location. An array is defined as the derived Data Structure that stores primitive data type elements like int, char, float, etc., in contiguous memory.

To delete an element from the Array, a user needs to provide us with either the particular element the user wants us to delete or the position whose Element the user wants to remove. One of them needs to be provided by the user to go ahead. Also, the position provided by the user must be greater than or equal to 0 and lesser than the size of the Array.

Next, we will explain three ways to delete an element from the Array in the c language.

Read about, Tribonacci Series and Short int in C Programming

Delete an element from the Array-based on position

The user needs to enter a particular position to remove from the Array in this C program. The user will provide the array size, array elements, and element position to be removed. The program inserts elements in the Array using a for a loop. Once all elements are inserted in the Array. Particular Index to delete provided from the user. The delete operation uses a for loop starting from the Particular Index given to the Array's length-1. We shift each value to the left by one, i.e. (arr[i] = arr[i + 1]). If the particular Index is not in range, i.e. between 0 to length-1, the program returns “Not a valid Index”.

Code:-

// Include the header files.
#include <stdio.h>

// Defining the main function. 
int main()
{

// Declaring the array and int variable Position and Length
int Array[25], Index, i, Length, Element;

// Ask user for array size
printf("\nEnter the length of the Array  :-   ");

// Taking user input and storing in variable Length.
scanf("%d", &Length);

printf("\n Enter %d elements of the Array :- \n", Length);

// Using a for loop for taking elements of array as input.
for (i = 0; i < Length; i++) {

	// For each iteration an element is asked from user and inserted in array
	scanf("%d", &Array[i]);
}  
   
printf("\n Enter the index to be deleted  :-  \n");
scanf("%d", &Index);
if(Index < 0 || Index >= Length)  {
 	printf("\n Not a Valid Index \n");
	return 0;
} 

else {

   // For each iteration elements are shifted to left 
    Element = Array[Index];
   	for (i = Index; i < Length; i++)  {
         Array[i] = Array[i + 1];
    }
    Length = Length - 1;
}

// Printing the final array
printf("\n Final Array after deletion of %d :\n",Element);
printf("[");
for (i = 0; i < Length; i++)    {
    if (i==Length-1) {
           printf("%d",Array[i]);
    } 
    else {
           printf("%d,", Array[i]);
    }
}
printf("]");

// Returning from main function
return 0;
}

Output 01:-

Enter the length of the Array:-   5
Enter 5 elements of the Array:- 
15
21
26
18
13
Enter the Index to be deleted:-  
3
Final Array after deletion of 18 :
[15,21,26,13]

Output 02:-

Enter the length of the Array:-   5
Enter 5 elements of the Array:- 
11
16
15
22
28
Enter the Index to be deleted:-  
9
Not a Valid Index

Delete an element from the Array-based on value.

In this C program, The user needs to enter the Element to be removed from the Array. The user will provide the array size, array elements, particular elements to be deleted. The program inserts elements in the Array using a for a loop. Once all elements are inserted in the Array. Element to delete provided from the user. Particular Index needs to be found for that Element once found to save it in variable Index and perform delete operation. The program returns Element not Found if the Element is not found in the entire array traversal.

Code:-

// Including the header files.
#include <stdio.h>

// Defining the main function. 
int main()
{

// Declaring the array and int variable Position and Length
int Array[25], Index, i, Length, Element;

// Ask user for array size
printf("\nEnter the length of the Array  :-   ");

// Taking user input and storing in variable Length.
scanf("%d", &Length);
printf("\n Enter %d elements of the Array :- \n", Length);

// Using a for loop for taking elements of array as input.
for (i = 0; i < Length; i++) {
  
   	  // For each iteration an element is asked from user and inserted in array
     scanf("%d", &Array[i]);
}     
   
// Ask user for the element to be deleted
printf("\n Enter the element to be deleted  :-  \n");
scanf("%d", &Element);
    
// Find the index of Element in the array
for (i = 0;i< Length;i++)	 {
     if (Array[i] == Element) 	{
         Index = i;
         break;
     } 
     else {
            
         // If Element is not valid index stores -1 value
         Index = -1;
     }
}
    
// If Element found delete else inform user Element not found
if (Index == -1) 	{
    printf("\n Element Not Found\n");
    return 0;
} 
else {
    for (i = Index; i < Length; i++) {
    	Array[i] = Array[i + 1];
	}
    Length = Length - 1;
}

// Printing the final array
printf("\n Final Array after deletion of %d :-\n",Element);
printf("[");
for (i = 0; i < Length; i++) {
     if (i==Length-1){
        printf("%d",Array[i]);
     } 
     else {
       printf("%d,", Array[i]);
     }
}
printf("]");

// Returning from main function
return 0;
}

Output 01:-

Enter the length of the Array:-   5
Enter 5 elements of the Array:- 
10
20
30
40
50
Enter the Element to be deleted:-  
40
Final Array after deletion of 40 :
[10,20,30,50]

Output 02:-

Enter the length of the Array:-   5
Enter 5 elements of the Array:- 
10
20
30
40
50
Enter the Element to be deleted:-  
60
Element Not Found

You can also read about dynamic array in c and C Static Function.

Delete an element from the Array by user-defined functions.

The user needs to enter a particular position to remove from the Array in this C program. The user will provide the array size, array elements, and element position to be removed. The program inserts elements in the Array using a for a loop. Once all elements are inserted in the Array. Particular Index to delete provided from the user. The DELETE _ELEMENT function uses a for loop starting from the Particular Index given to the Array's length-1. We shift each value to the left by one, i.e. (arr[i]=arr[i+1]). If the Particular Index is not in range, i.e. between 0 to length-1, the program prints Not a valid Index. After successful deletion, the program prints an array using the PRINT_ARRAY function.

Code:-

// Including Header files
# include <stdio.h>

// Defining Delete_Element function to delete the element present at Index 
void Delete_Element (int *Array, int Length, int Index)  
{  
    int i;  
    
    // Using for loop to update the index  
    // For each iteration elements are shifted to left 
    for (i = Index; i < Length; i++)  
    {  
        Array[i] = Array[i+1];   
    }  
    return;
}  

// Defining the Print_Array function
void Print_Array (int *Array,int Length){
    int i;
    printf("\n Final Array after deletion :- \n");
  	printf("[");
  	for (i = 0; i < Length; i++)
   	{
       if (i==Length-1){
           printf("%d",Array[i]);
       }
       else{
           printf("%d,", Array[i]);
       }
    }
   printf("]");
   return;
} 

// Defining the main function. 
int main()
{

// Declaring the array and int variable Position and Length
int Array[25], Index, i, Length, Element;

// Ask user for array size
printf("\nEnter the length of the Array  :-   ");

// Taking user input and storing in variable Size.
scanf("%d", &Length);
printf("\n Enter %d elements of the Array :- \n", Length);

// Using a for loop for taking elements of array as input.
for (i = 0; i < Length; i++) 	{
   
   	 // For each iteration an element is asked from user and inserted in array
     scanf("%d", &Array[i]);
 }     
printf("\n Enter the Index to be deleted  :-  \n");
scanf("%d", &Index); 
if(Index < 0 || Index >= Length)    {
   printf("\n Not a Valid Index \n");
   return 0;
   } 
   else {
   Element = Array[Index];
   Delete_Element(Array,Length,Index);
   Length--;
}

// Printing the final array
  Print_Array(Array,Length);
 
// Returning from main function
return 0;
}

Output 01:-

Enter the length of the Array:-   5
Enter 5 elements of the Array:- 
10
20
30
40
50
Enter the Index to be deleted:-  
2
Final Array after deletion:- 
[10,20,40,50]

Output 02:-

Enter the length of the Array:-   5
Enter 5 elements of the Array:- 
10
20
30
40
50
Enter the Index to be deleted:-  
10
Not a Valid Index 

 

You can practice yourself with this online C compiler easily.

FAQs

  1. How to delete an element from the Array in c language?
    To delete an element from the Array, a user needs to provide us with either the particular element the user wants us to delete or the position whose Element the user wants to remove. One of them needs to be provided by the user to go ahead. Also, the position provided by the user must be greater than or equal to 0 and lesser than the size of the Array. Then deletion could be performed using a standard programme or creating a method function for operations.
     
  2. What is an array?
    An array is a collection of similar data type elements stored in a contiguous memory location.
     
  3. What is the data type of an array?
     An array is defined as the derived data type that stores primitive data type elements like int, char, float, etc., in contiguous memory.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Key Takeaways

In this article, we have extensively discussed the different ways to delete an element from the Array. We have also explored the various methods of removing the Element based on whether users provide a particular position to remove or a particular character.
We hope that this blog has helped you enhance your knowledge regarding the C programming language. If you want to learn more about such content, practice some quality questions that require you to excel your preparations. In that case, you can visit our Guided Path in  Coding Ninjas Studio.To be more confident in data structures and algorithms, try out our DS and Algo Course. Until then, All the best for your future endeavours, and Keep Coding.

Check this out, Array in Javascript

Previous article
C Program For Inserting an Element In an Array
Next article
2's complement
Live masterclass