C Program to Delete an Element in an Array Last Updated : 02 Apr, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we will learn to how delete an element from an array in C. C arrays are static in size, it means that we cannot remove the element from the array memory. But we can logically delete the element by overwriting it and updating the size variable.Delete the Given Element from an ArrayTo delete an element using its value from an array, first find the position of the element using a loop and then shift all the element to the right of it one position to the left. Let's take a look at an example: C #include <stdio.h> void del(int arr[], int *n, int key) { // Find the element int i = 0; while (arr[i] != key) i++; // Shifting the right side elements one // position towards left for (int j = i; j < *n - 1; j++) { arr[j] = arr[j + 1]; } // Decrease the size (*n)--; } int main() { int arr[] = {10, 20, 30, 40, 50}; int n = sizeof(arr) / sizeof(arr[0]); int key = 30; // Delete the key from array del(arr, &n, key); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; } Output10 20 40 50 Explanation: In the given program, the function del() deletes an element from the array arr[] based on the provided key. It first finds the position of the key using a loop. Once found, all elements to the right of the key are shifted one position to the left to overwrite the deleted element. Finally, the size of the array is decremented by modifying the value pointed to by n.Time Complexity: O(n) (for Searching) + O(n) (for Shifting) + O(1) (for decrementing size) = O(n)Auxiliary Space: O(1)Delete the Last Element from an ArrayIn the above method, we can get a special case where we don't need to shift any element. It's the case where the element to be deleted is present at last. But we still need to find the position of the element, which will take O(n) time. But what if we are already told that we have to delete the last element?In this case, we can just decrease the size of the array without shifting the element leading to O(1) time for deletion. The below example demonstrates that: C #include <stdio.h> void delLast(int arr[], int *n) { // Decrease the size (*n)--; } int main() { int arr[] = {10, 20, 30, 40, 50}; int n = sizeof(arr) / sizeof(arr[0]); // Delete the last element delLast(arr, &n); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; } Output10 20 30 40 Time Complexity: O(1) (for decrementing size)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article C Program to Delete an Element in an Array D deepanshus2qvy Follow Improve Article Tags : C Programs C Language C Examples Similar Reads How to Delete an Element from an Array of Structs in C? In C, an array of structs refers to the array that stores the structure variables as its elements. In this article, we will learn how to delete an element from an array of structures in C. For Example, Input: struct Person persons[3] = { { "Person1", 25 }, { "Person2", 30 }, { "Person3", 22 }, }; Ta 2 min read C Program to Remove All Occurrences of an Element in an Array To remove all the occurrences of an element in an array, we will use the following 2 approaches: Using MethodsWithout using methods or With Conditional Statement We will keep the same input in all the mentioned approaches and get an output accordingly. Input: array = {1, 2, 1, 3, 1} value = 1 Output 3 min read C Program To Delete Alternate Nodes Of A Linked List Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Recomm 3 min read C Program to Implement Queue using Array A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle which means the elements added first in a queue will be removed first from the queue. In this article, we will learn how to implement a Queue using Array in C. Implement Queue using Array in CTo implement Queue u 6 min read C Program For Writing A Function To Delete A Linked List Algorithm For C:Iterate through the linked list and delete all the nodes one by one. The main point here is not to access the next of the current pointer if the current pointer is deleted. Implementation: C // C program to delete a linked list #include<stdio.h> #include<stdlib.h> #includ 2 min read C Program For Deleting A Node In A Linked List We have discussed Linked List Introduction and Linked List Insertion in previous posts on a singly linked list.Let us formulate the problem statement to understand the deletion process. Given a 'key', delete the first occurrence of this key in the linked list. Iterative Method:To delete a node from 3 min read C Program For Deleting A Linked List Node At A Given Position Given a singly linked list and a position, delete a linked list node at the given position. Example: Input: position = 1, Linked List = 8->2->3->1->7 Output: Linked List = 8->3->1->7 Input: position = 0, Linked List = 8->2->3->1->7 Output: Linked List = 2->3->1 3 min read C Program to Remove Duplicates from Sorted Array In this article, we will learn how to remove duplicates from a sorted array using the C program.The most straightforward method is to use the two-pointer approach which uses two pointers: one pointer to iterate over the array and other to track duplicate elements. In sorted arrays, duplicates are ad 4 min read How to Create a Dynamic Array Inside a Structure? In C, the structure can store the array data types as one of its members. In this article, we will learn how to create a dynamic array inside a structure in C. Creating a Dynamic Array Inside a Structure in CTo create a dynamic array inside a structure in C, define a structure that contains a pointe 2 min read How to Find Maximum Value in an Array in C? In C, arrays are data structures that allow the user to store a collection of data of the same type. In this article, we will learn how we can find the maximum value in an array in C. Example Input: arr = {5,3,1,2,4} Output: The maximum value of the array is: 5Finding Maximum Value in an Array in CW 2 min read Like