C Program to delete element at end of Array

To delete an element from the end in an array, we will just reduce the size of an array by one. After reducing the size we will print the array and will find that the last element is not printing. It means that the element is not in the array now.

Introduction to Arrays in C

In C, an array is a collection of elements of the same type, stored in contiguous memory locations. Arrays are indexed starting from 0, making them easily accessible using their indices. However, C arrays have a fixed size, meaning that once an array is declared, its size cannot be dynamically changed. This limitation requires specific strategies for manipulating array elements, such as deleting an element.

Deleting the Last Element

Deleting the last element of an array doesn’t literally remove the element from memory but involves logically reducing the array’s size by one. This means we consider the array to be one element shorter than before, effectively ignoring the last element. Here’s how you can implement this in C:

Step-by-Step Explanation

  1. Declare the Array and Its Size: Start by defining the array and initializing it with elements. Also, maintain a variable to keep track of the current size of the array.
  2. Display the Original Array: Print the array elements, so there’s a clear understanding of what the array looks like before modification.
  3. Delete the Last Element: Reduce the size of the array by one to ‘delete’ the last element.
  4. Display the Modified Array: Print the array again to show that the last element has been effectively removed.

C Program to delete element at end of Array

#include <stdio.h>
void main()
{
    int position, i, n, value,ch;
    printf("C Program to delete element at end of Array\n");
    printf("First enter number of elements you want in Array\n");
    scanf("%d", &n);
    int arr[n];
   for(i = 0; i < n; i++)
    {
        printf("Please give value for index %d : ",i);
        scanf("%d",&arr[i]);
    }
    value=arr[n-1]; //assigning last value in value variable
    printf("Element %d is deleting at %d index \n",value,n-1);
    n=n-1;//here decreasing value to reduce size of array
    printf("New Array after deleting element at end \n ");
    for(i = 0; i < n; i++)
    {
       printf("%d \t",arr[i]);
    }
}

Output

C Program to delete element at end of Array
First enter number of elements you want in Array
6
Please give value for index 0 : 3
Please give value for index 1 : 2
Please give value for index 2 : 1
Please give value for index 3 : 4
Please give value for index 4 : 5
Please give value for index 5 : 3
Element 3 is deleting at 5 index 
New Array after deleting element at end 
 3      2       1       4       5 

Considerations

What Happens to the Deleted Element?

In C, the “deleted” element is not actually removed from memory. It still exists but is no longer considered part of the array when we reduce the array’s size. This element is typically overwritten when new data is stored in the array.

Efficiency

Deleting the last element of an array is an O(1) operation, meaning it takes constant time. It’s efficient because no elements need to be shifted in memory.

Limitations

If you need to frequently add and remove elements from an array, especially from positions other than the end, a different data structure, such as a linked list, might be more appropriate due to the limitations of fixed-size arrays in C.