C Program to Delete an Element in an Array

C program to delete an element in an array | Here we will write a program to remove an element from an array using position.

Take an array of numbers and also take the position/element to delete. If you want to delete an element based on its value (like among [10, 20, 30, 40] you want to delete 20) then the first search for the element and find its position.

Delete array element by its index in C

To delete the array element by its index in the C programming language we will use another array and loops. The operations can be performed as,

a) Take an array and the index.
b) Check index is valid or not.
b) Create a new array with size = original_array_size – 1
c) Copy elements of the original array to the new array from 0 to index position.
d) Skip the copying element at the index position.
e) Copy the remaining element of the original array.

#include<stdio.h>
// function to display array
void display(int arr[], int n)
{
  for (int i = 0; i < n; ++i)
    printf("%d ", arr[i]);
}

// main function
int main()
{
  // original array
  int arr[] = {10, 20, 30, 40, 50};

  // take index to remove
  int index;
  printf("Enter index: ");
  scanf("%d", &index);

  // calculate size of the array
  int n = sizeof(arr)/sizeof(arr[0]);

   // check index is valid or not
  if(index < 0 || index > n-1)
  {
    printf("Error! the index is not valid.");
    printf("\nEnter index from 0 to %d\n", n-1);
    return 0;
  }

  // display original array
  printf("Original array: ");
  display(arr, n);

  // create new array of size = n-1
  int temp[n-1];

  // copy elements
  for (int i=0, j=0; i < n; ++i)
  {
    // skip at index position
    if(i == index) continue;
    else temp[j++] = arr[i];
  }
  
  // display new array
  printf("\nNew Array: ");
  display(temp, n-1);

  return 0;
}

Output for the different test-cases:-

Enter index: 4
Original array: 10 20 30 40 50
New Array: 10 20 30 40

Enter index: 0
Original array: 10 20 30 40 50
New Array: 20 30 40 50

Enter index: 3
Original array: 10 20 30 40 50
New Array: 10 20 30 50

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

Enter index: 6
Error! the index is not valid.
Enter index from 0 to 4

Enter index: -3
Error! the index is not valid.
Enter index from 0 to 4

We can’t delete an element when a position is less than zero or greater than N (last element position). If a user tries to delete those positions then program display “Error! index is not valid.”. For the correct position, it will delete an element at that position.

Delete element by its value

In this problem, first we need to search for the element and find its index. If the element is not found then display error message else perform the deletion operation. To search for the element we can use linear search in C or binary search in C. The binary search is applicable only if the array is in ascending order.

C Program to Delete an Element in an Array by its value

#include<stdio.h>
// function to display array
void display(int arr[], int n)
{
  for (int i = 0; i < n; ++i)
    printf("%d ", arr[i]);
}

// function for linear search
/* if match found then return index of 
   search key else return -1 */
int linearSearch(int arr[], int n, int key) 
{
  for(int i=0; i<n; i++) 
  {
    if(key == arr[i])
      return i;
  }
  return -1;
}

// main function
int main()
{
  // original array
  int arr[] = {50, 30, 40, 10, 20};

  // take element to remove
  int key;
  printf("Enter element: ");
  scanf("%d", &key);

  // calculate size of the array
  int n = sizeof(arr)/sizeof(arr[0]);

  // search index of the element
  int index = linearSearch(arr, n, key);

   // check index is valid or not
  if(index == -1)
  {
    printf("%d not found in the array", key);
    return 0;
  }

  // display original array
  printf("Original array: ");
  display(arr, n);

  // create new array of size = n-1
  int temp[n-1];

  // copy elements
  for (int i=0, j=0; i < n; ++i)
  {
    // skip at index position
    if(i == index) continue;
    else temp[j++] = arr[i];
  }
  
  // display new array
  printf("\nNew Array: ");
  display(temp, n-1);

  return 0;
}

Output:-

Enter element: 10
Original array: 50 30 40 10 20
New Array: 50 30 40 20

Enter element: 30
Original array: 50 30 40 10 20
New Array: 50 40 10 20

Enter element: 90
90 not found in the array

If the array contains duplicate elements then the linearSearch() method will always return the index of the first matched element, therefore only the first matched element will be deleted.

// original array
int arr[] = {50, 50, 40, 20, 20};

Output:-

Enter element: 50
Original array: 50 50 40 20 20
New Array: 50 40 20 20

Enter element: 20
Original array: 50 50 40 20 20
New Array: 50 50 40 20

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *