C program to Delete an Element from an Array

An array is a collection of similar data elements stored in a contiguous memory location.

Example: arr[6] = {12,65,32,75,48,11}

Value:  12  65  32  75  48  11
               ↑    ↑    ↑     ↑    ↑     ↑
Index:   0    1    2    3    4     5

The Element we are deleting here is “75”.

Original Array:

12 65 32 75 48 11

New Array:

12 65 32 48 11
Problem Description

Write a C Program to delete an element in an Array by index or value.

Problem Solution
advertisement
advertisement

1. Create an array of some size, and fill up its elements.
2. Take a value as input from users, which needs to be deleted.
3. Using for loop, check whether the value is present in the array or not.
4. If the value is present, then save its location and if its not present, some appropriate message get printed.
5. Again, the loop runs from that saved position till the end of array, causing each element to shift left by one step. This, way the value get deleted.
6. Print the array.

There are several ways to delete the element from an array in C language. Let’s take a detailed look at all the approaches to delete an array element.

Method 1: (Delete Element by Index)

In this approach, we will use a loop to iterate through the array and delete the element from the array.

Note: Join free Sanfoundry classes at Telegram or Youtube

Example:
Input:
Size of array = 5
array = [8, 4, 9, 6, 2] Index of the element to be deleted is “3”

Output:
[8, 4, 9, 2]

Program/Source Code

Here is source code of the C program to delete the specified integer from an array by index. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1. /*
  2.  * C program to delete an element from an array by index
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. int main(void)
  9. {
  10.     int i, n, index, arr[10];
  11.     printf("Enter the size of the array: ");
  12.     scanf("%d", &n);
  13.     printf("Enter the elements of the array: \n");
  14.     for (i = 0; i < n; i++)
  15.     {
  16.         printf("arr[%d] = ", i);
  17.         scanf("%d", &arr[i]);
  18.     }
  19.     printf("Enter the index of the element to be deleted: ");
  20.     scanf("%d", &index);
  21.     if (index >= n+1)  
  22.     {  
  23.         printf (" \n Deletion is not possible in the array.");  
  24.     }  
  25.     else  
  26.     {  
  27.         for (i = index; i < n - 1; i++)
  28.             arr[i] = arr[i + 1];
  29.             printf("The array after deleting the element is: ");
  30.         for (i = 0; i < n - 1; i++)
  31.             printf("%d ", arr[i]);
  32.         return 0;
  33.     }
  34. }
Program Explanation

1. The program will ask the user to enter the size of the array and then the elements of the array and store it in n and &arr[i].
2. Ask the user to enter the index of the element to be deleted.
3. Loop through the array and delete the element from the array.
4. Print the array.

advertisement

Time Complexity: O(n)
The above program for deleting an element in an array has a time complexity of O(n), as the for loop runs from 0 to n.

Space Complexity: O(n)
In the above program, space complexity is O(n) as an array of size n has been initialized to store the values in it.

Runtime Test Cases

Testcase 1: In this case, the index of the element to be deleted is “3”

Enter the size of the array: 5
Enter the elements of the array: 
arr[0] = 8
arr[1] = 4
arr[2] = 9
arr[3] = 6
arr[4] = 2
Enter the index of the element to be deleted: 3
The array after deleting the element is: 8 4 9 2

Testcase 2: In this case, the index of the element to be deleted is “6”

advertisement
Enter the size of the array: 4
Enter the elements of the array: 
arr[0] = 10
arr[1] = 4
arr[2] = 6
arr[3] = 1
Enter the index of the element to be deleted: 6
Deletion is not possible in the array.

Method 2: (Delete Element by Value)

In this approach, we will use a loop to iterate through the array and delete the element from the array.

Example:
Input:
Size of array = 4
array = [12, 15, 33, 19] Value of the element to be deleted is “15”

Output:
[12, 33, 19]

Program/Source Code

Here is source code of the C program to delete the specified element from an array. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1. /*
  2.  * C program to delete an element from an array by value
  3.  */
  4.  
  5. #include <stdio.h>
  6. void main()
  7. {
  8.     int arr[10];
  9.     int i, n, pos, element, found = 0;
  10.  
  11.     printf("Enter the size of the array: ");
  12.     scanf("%d", &n);
  13.     printf("Enter the elements of the array: \n");
  14.     for (i = 0; i < n; i++)
  15.     {
  16.         printf("arr[%d] = ", i);
  17.         scanf("%d", &arr[i]);
  18.     }
  19.  
  20.     printf("Enter the value of the element to be deleted:\n");
  21.     scanf("%d", &element);
  22.  
  23.     for (i = 0; i < n; i++)
  24.     {
  25.         if (arr[i] == element)
  26.         {
  27.             found = 1;
  28.             pos = i;
  29.             break;
  30.         }
  31.     }
  32.  
  33.     if (found == 1)
  34.     {
  35.         for (i = pos; i <  n - 1; i++)
  36.         {
  37.             arr[i] = arr[i + 1];
  38.         }
  39.  
  40.         printf("The array after deleting the element is: \n");
  41.         for (i = 0; i < n - 1; i++)
  42.         {
  43.             printf("%d\n", arr[i]);
  44.         }
  45.  
  46.     }
  47.     else
  48.     printf("Element %d is not found in the array\n", element);
  49. }
Program Explanation

1. Declare an array, arr[10] of some fixed capacity, 10.
2. Take size of the array as input from users.
3. Using for loop, define the elements of the array.
4. Now, take a number form users as input, which needs to be deleted.
5. Run a for loop, comparing each element of the array to that number if both have same magnitude.
6. If the number is present in the array, then save its location. If number is not present in the array, then print appropriate message.
7. Run a for loop from that saved location to the size of array, shifting each element of the array leftwards by one. This way the number gets deleted.
9. Print the array.

Time Complexity: O(n)
The above program for deleting an element in an array has a time complexity of O(n), as the for loop runs from 0 to n.

Space Complexity: O(n)
In the above program, space complexity is O(n) as an array of size n has been initialized to store the values in it.

Runtime Test Cases

Testcase 1: In this case, the value of the element to be deleted is “15”

Enter the size of the array: 4
Enter the elements of the array: 
arr[0] = 12
arr[1] = 15
arr[2] = 33
arr[3] = 19
Enter the value of the element to be deleted:
15
The array after deleting the element is: 
12
33
19

Testcase 2: In this case, the value of the element to be deleted is “15”

Enter the size of the array: 3
Enter the elements of the array: 
arr[0] = 5
arr[1] = 2
arr[2] = 6
Enter the value of the element to be deleted:
9
Element 9 is not found in the array

Method 3: (Advanced Approach)

In this approach we’ll use separate functions to delete elements by index or by value and give back the array after deleting the element.

Methods used:

  • delete_element_by_index() – This function will delete the element from the array by index.
  • delete_element_by_value() – This function will delete the element from the array by value.
  • print_array() – This function will print the array.
Program/Source Code

Here is source code of the C program to delete the specified element from an array by index or value. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1. /*
  2.  * C program to delete an element from an array by index or value
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. int n;
  9.  
  10. void delete_element_by_index(int arr[], int index)
  11. {
  12.     int i;
  13.     for (i = index; i < n - 1; i++)
  14.         arr[i] = arr[i + 1];
  15.     n -= 1;
  16. }
  17.  
  18. void delete_element_by_value(int arr[], int value)
  19. {
  20.     int i;
  21.     for (i = 0; i < n; i++)
  22.     {
  23.         if (arr[i] == value)
  24.         {
  25.             for (; i < n - 1; i++)
  26.                 arr[i] = arr[i + 1];
  27.                 n -= 1;
  28.                 break;
  29.         }
  30.     }
  31. }
  32.  
  33. void print_array(int arr[])
  34. {
  35.     int i;
  36.     printf("\n[ ");
  37.     for (i = 0; i < n; i++)
  38.         printf("%d ", arr[i]);
  39.     printf("]\n");
  40. }
  41.  
  42. void init_array(int arr[])
  43. {
  44.     int i;
  45.     for (i = 0; i < n; i++)
  46.     {
  47.         printf("%d: ", i);
  48.         scanf("%d", &arr[i]);
  49.     }
  50. }
  51.  
  52. int main(void)
  53. {
  54.     int index, value, arr[10], choice;
  55.     printf("Enter the size of the array: ");
  56.     scanf("%d", &n);
  57.     printf("Enter the elements of the array: \n");
  58.     init_array(arr);
  59.     printf("You can delete an element by index or value.\n1. Delete by index\n2. Delete by value\n");
  60.     printf("Enter your choice: ");
  61.     scanf("%d", &choice);
  62.     if (choice == 1)
  63.     {
  64.         printf("Enter the index of the element to be deleted: ");
  65.         scanf("%d", &index);
  66.         delete_element_by_index(arr, index);
  67.     }
  68.     else
  69.     {
  70.         printf("Enter the value of the element to be deleted: ");
  71.         scanf("%d", &value);
  72.         delete_element_by_value(arr, value);
  73.     }
  74.     print_array(arr);
  75. }
Program Explanation

1. The program will ask the user to enter the size of the array and then the elements of the array.
2. Ask the user to enter the index of the element to be deleted.
3. The program will provide a choice between deleting element by value or by index.
4. Then call the function delete_element_by_index() to delete the element from the array.
5. Then program will call the function print_array() to print the array.
6. The program will then ask the user to enter the value of the element to be deleted.
7. The program will then call the function delete_element_by_value() to delete the element from the array.
8. The program will then call the function print_array() to print the array.

Time Complexity: O(n)
The above program for deleting an element in an array has a time complexity of O(n), as the for loop runs from 0 to n.

Space Complexity: O(n)
In the above program, space complexity is O(n) as an array of size n has been initialized to store the values in it.

Runtime Test Cases

Testcase 1: In this case, the index of the element to be deleted is “2”

Enter the size of the array: 4
Enter the elements of the array: 
arr[0] = 2
arr[1] = 8
arr[2] = 4
arr[3] = 9
You can delete an element by index or by value.
1. Delete by index
2. Delete by value
Enter your choice: 1
Enter the index of the element to be deleted: 2
 
[ 2 8 9 ]

Testcase 2: In this case, the value of the element to be deleted is “32”

Enter the size of the array: 6
Enter the elements of the array: 
arr[0] = 12
arr[1] = 65
arr[2] = 32
arr[3] = 75
arr[4] = 48
arr[5] = 11
You can delete an element by index or by value.
1. Delete by index
2. Delete by value
Enter your choice: 2
Enter the value of the element to be deleted: 32
 
[ 12 65 75 48 11 ]

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.