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 |
Write a C Program to delete an element in an Array by index or value.
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.
- Delete an Element from an Array by Index
- Delete an Element from an Array by Value
- Delete an Element from an Array by Index or 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 = 5
array = [8, 4, 9, 6, 2]
Index of the element to be deleted is “3”
Output:
[8, 4, 9, 2]
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.
/*
* C program to delete an element from an array by index
*/
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, n, index, arr[10];
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements of the array: \n");
for (i = 0; i < n; i++)
{
printf("arr[%d] = ", i);
scanf("%d", &arr[i]);
}
printf("Enter the index of the element to be deleted: ");
scanf("%d", &index);
if (index >= n+1)
{
printf (" \n Deletion is not possible in the array.");
}
else
{
for (i = index; i < n - 1; i++)
arr[i] = arr[i + 1];
printf("The array after deleting the element is: ");
for (i = 0; i < n - 1; i++)
printf("%d ", arr[i]);
return 0;
}
}
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.
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.
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”
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.
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]
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.
/*
* C program to delete an element from an array by value
*/
#include <stdio.h>
void main()
{
int arr[10];
int i, n, pos, element, found = 0;
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements of the array: \n");
for (i = 0; i < n; i++)
{
printf("arr[%d] = ", i);
scanf("%d", &arr[i]);
}
printf("Enter the value of the element to be deleted:\n");
scanf("%d", &element);
for (i = 0; i < n; i++)
{
if (arr[i] == element)
{
found = 1;
pos = i;
break;
}
}
if (found == 1)
{
for (i = pos; i < n - 1; i++)
{
arr[i] = arr[i + 1];
}
printf("The array after deleting the element is: \n");
for (i = 0; i < n - 1; i++)
{
printf("%d\n", arr[i]);
}
}
else
printf("Element %d is not found in the array\n", element);
}
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.
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
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.
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.
/*
* C program to delete an element from an array by index or value
*/
#include <stdio.h>
#include <stdlib.h>
int n;
void delete_element_by_index(int arr[], int index)
{
int i;
for (i = index; i < n - 1; i++)
arr[i] = arr[i + 1];
n -= 1;
}
void delete_element_by_value(int arr[], int value)
{
int i;
for (i = 0; i < n; i++)
{
if (arr[i] == value)
{
for (; i < n - 1; i++)
arr[i] = arr[i + 1];
n -= 1;
break;
}
}
}
void print_array(int arr[])
{
int i;
printf("\n[ ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("]\n");
}
void init_array(int arr[])
{
int i;
for (i = 0; i < n; i++)
{
printf("%d: ", i);
scanf("%d", &arr[i]);
}
}
int main(void)
{
int index, value, arr[10], choice;
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements of the array: \n");
init_array(arr);
printf("You can delete an element by index or value.\n1. Delete by index\n2. Delete by value\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 1)
{
printf("Enter the index of the element to be deleted: ");
scanf("%d", &index);
delete_element_by_index(arr, index);
}
else
{
printf("Enter the value of the element to be deleted: ");
scanf("%d", &value);
delete_element_by_value(arr, value);
}
print_array(arr);
}
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.
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”.
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Practice BCA MCQs
- Check Computer Science Books
- Check C Books