C Program to Delete an Element from an Array

  • Write a C program to delete an element from an array.

Given an array of length N, we have to delete an element at index i (0 <= i <= N-1) from an array. After inserting an element, the number of elements in array will decrease by one.
All elements of array are stored in contiguous memory location. To delete an element at index i in array we have to shift all elements from index i+1 to N-1 to previous index. An element at index k, will be moved to index k-1.

For Example
Suppose we have an array "inputArray" of length 9, which contains 6 elements from inputArray[0] to inputArray[5]. We have to delete element at index 2. Move every element from inputArray[3] to inputArray[5] to previous index in array.
Move inputArray[3] to inputArray[3],
Move inputArray[4] to inputArray[3],
Move inputArray[5] to inputArray[4].
This will delete inputArray[2] by overwriting it with inputArray[4].
Now total number of elements in inputArray becomes 5 from index 0 to 4.

Algorithm to delete an element from an array
Let inputArray is an array of length N, which contains M(M<=N) elements and we want to delete an element at index I.
  • Move all elements from inputArray[I+1] to inputArray[M-1] to previous index.
  • Move inputArray[j] to inputArray[j - 1], I+1 <= j <= M-1.
  • Above step delete inputArray[I] by overwriting it with inputArray[I+1].
  • Now, inputArray contains M-1 elements from index 0 to M-2.
The time complexity of deleting an element from an array is O(n)

C program to delete an element from an array

Below program first takes an integer array containing 'elementCount' elements as input from user using a for loop and scanf function. Next, it takes the index of the number to be deleted from array as input from user. Then using a for loop, it shifts all the elements after 'index' by one position as explained above. After shifting, the element earlier present at index gets overwritten and the number of elements in inputArray decreases by one.

/*
* C Program to delete an element from an array
*/
#include <stdio.h>
#include <conio.h>

int main(){
    int inputArray[500], elementCount, counter, index;
    
    printf("Enter number of elements in array: ");
    scanf("%d", &elementCount);
    printf("Enter %d numbers \n", elementCount);
    
    for(counter = 0; counter < elementCount; counter++){
        scanf("%d", &inputArray[counter]);
    }
    
    printf("Enter index from where you want to delete an element\n");
    scanf("%d", &index);
    
    /* Move all elements right of index to left by one position */
    for(counter = index; counter < elementCount - 1; counter++){
        inputArray[counter] = inputArray[counter+1];
    } 
    /* Print updated array */
    printf("Updated Array\n");
    for(counter = 0; counter < elementCount - 1; counter++){
        printf("%d ", inputArray[counter]);
    }
        
    getch();
    return 0;
}

Program Output
Enter number of elements in array: 5
Enter 5 numbers 
1 2 3 4 5
Enter index from where you want to delete an element
2
Updated Array
1 2 4 5
Enter number of elements in array: 6
Enter 5 numbers 
1 2 3 4 5 6
Enter index from where you want to delete an element
0
Updated Array
2 3 4 5 6

Related Topics
C program to insert an element in an array
C program to find second largest element in array
C program to delete duplicate elements from an array
C program to find sum of array elements using recursion
C program to delete duplicate elements from an array
C program to find second largest element in array
C program to find sum of digits of a number using recursion
C program to check year is leap year or not
List of all C Programs