๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ c-program-to-delete-an-element-in-an-array
C Program to Delete an Element in an Array - GeeksforGeeks
April 2, 2025 - 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 ...
๐ŸŒ
W3Schools
w3schools.in โ€บ c-programming โ€บ examples โ€บ delete-element-from-array
C Program to Delete an Element from an Array - W3schools
For example if array is containing five elements and you want to delete element at position six which is not possible. ... #include <stdio.h> int main() { int array[100], position, c, n; printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); printf("Enter the location where you wish to delete element\n"); scanf("%d", &position); if ( position >= n+1 ) printf("Deletion not possible.\n"); else { for ( c = position - 1 ; c < n - 1 ; c++ ) array[c] = array[c+1]; printf("Resultant array is\n"); for( c = 0 ; c < n - 1 ; c++ ) printf("%d\n", array[c]); } return 0; }
๐ŸŒ
Sanfoundry
sanfoundry.com โ€บ c-program-delete-specified-integer-array
C program to Delete an Element from an Array - Sanfoundry
May 15, 2023 - 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.
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ c-program-delete-element-array
C Program to Delete an Element in an Array
April 5, 2025 - First, we used the If statement to check whether the user-specified index position is less than 0 or greater than or equal to size. If it is true, then it will print an error message. Otherwise, it will enter into the Else Block. Within the Else block, we used For Loop. It will make sure that the position values are between the Position and the maximum size value of the One Dimensional Array. User Entered Values for the Program to Delete an Element in an Array example: Size = 4, Elements = 25 69 73 224, and Position = 2.
๐ŸŒ
w3resource
w3resource.com โ€บ c-programming-exercises โ€บ array โ€บ c-array-exercise-15.php
C Program: Delete an element at a specified position
Delete an element at desired position from an array : --------------------------------------------------------- Input the size of array : 5 Input 5 elements in the array in ascending order: element - 0 : 1 element - 1 : 2 element - 2 : 3 element - 3 : 4 element - 4 : 5 Input the position where to delete: 3 The new list is : 1 2 4 5 ... Write a C program to delete an element from an array at a given position using pointer arithmetic.
๐ŸŒ
Programming Simplified
programmingsimplified.com โ€บ c โ€บ source-code โ€บ c-program-delete-element-from-array
C program to delete an element from an array | Programming Simplified
if (position >= n+1) printf("Deletion not possible.\n"); else { for (c = position - 1; c < n - 1; c++) array[c] = array[c+1]; ... Download Delete element from array program. You may have observed that we need to shift array elements which are after the element to be deleted, it's very inefficient ...
๐ŸŒ
CODEDOST
codedost.com โ€บ home โ€บ array&pointers in c โ€บ c program to delete an element from an array
C program to delete an element from an array | CODEDOST
July 15, 2018 - #include<stdio.h> void main() { int a[100],i,n,pos; printf("\nEnter no of elements\n"); scanf("%d",&n); printf("Enter the elements\n"); for (i=0;i<n;i++) { scanf("%d",&a[i]); } printf("Elements of array are\n"); for(i=0;i<n;i++) { printf("a[%d] = %d\n",i,a[i]); } printf("Enter the position from which the number has to be deleted\n"); scanf("%d",&pos); for(i=pos;i<n-1;i++) { a[i]=a[i+1]; } n=n-1; printf("\nOn Deletion, new array we get is\n"); for(i=0;i<n;i++) { printf("a[%d] = %d\n",i,a[i]); } }
๐ŸŒ
ProCoding
procoding.org โ€บ c-program-delete-an-element-from-array
C Program to delete an element from array - procoding.org
May 13, 2020 - An element can be deleted by shifting all the next element back to one position. #include <stdio.h> void main() { int arr[50], size, pos, i; printf("Enter number of elements: "); scanf("%d", &size); printf("Enter array elements-\n"); for (i ...
๐ŸŒ
Codeforwin
codeforwin.org โ€บ home โ€บ c program to delete element from an array
C program to delete element from an array - Codeforwin
July 20, 2025 - Copy the next element to the current element of array. Which is you need to perform array[i] = array[i + 1]. Repeat above steps till last element of array. Finally decrement the size of array by one.
Find elsewhere
๐ŸŒ
log2base2
log2base2.com โ€บ data-structures โ€บ array โ€บ remove-a-specific-element-from-array.html
Remove a specific element from array
/* * Program : Remove an element in the array * Language : C */ #include<stdio.h> #define size 5 int main() { int arr[size] = {1, 20, 5, 78, 30}; int key, i, index = -1; printf("Enter element to delete\n"); scanf("%d",&key); /* * iterate the array elements using loop * if any element matches ...
๐ŸŒ
CodeScracker
codescracker.com โ€บ c โ€บ program โ€บ c-program-delete-element-from-array.htm
C Program to Delete an Element from an Array
But to clarify, whether it actually ... version of the above program. #include<stdio.h> #include<conio.h> int main() { int arr[50], size, del, i, j, found=0; printf("How many element to store in Array ?...
๐ŸŒ
Java Guides
javaguides.net โ€บ 2023 โ€บ 09 โ€บ c-program-to-delete-element-from-array.html
C Program to Delete an Element from Array
September 12, 2023 - We will also provide a step-by-step explanation of the program. ... #include <stdio.h> int main() { int n, i, pos; int arr[100]; // Collecting the number of elements in the array printf("Enter the number of elements in array: "); scanf("%d", &n); // Obtaining the elements printf("Enter %d integers:\n", n); for(i = 0; i < n; i++) { scanf("%d", &arr[i]); } // Getting the position of the element to delete printf("Enter the position of element to delete (0 to %d): ", n-1); scanf("%d", &pos); // Deleting the element for(i = pos; i < n-1; i++) { arr[i] = arr[i+1]; } // Displaying the result printf("Resultant array is:\n"); for(i = 0; i < n-1; i++) { printf("%d ", arr[i]); } return 0; }
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ c-program-to-delete-an-array-element-using-pointers
C program to delete an array element using Pointers
Enter the size of array: 5 Enter the elements: 12 34 56 67 78 Original array: 12 34 56 67 78 Enter the position of element to be deleted (1 to 5): 3 After deletion the array elements are: 12 34 67 78 ยท Memory Allocation: Dynamic memory is allocated using malloc() to create an array of specified size ยท Pointer Arithmetic: Elements are accessed using *(a + i) notation instead of array subscript
Top answer
1 of 6
27

There are really two separate issues. The first is keeping the elements of the array in proper order so that there are no "holes" after removing an element. The second is actually resizing the array itself.

Arrays in C are allocated as a fixed number of contiguous elements. There is no way to actually remove the memory used by an individual element in the array, but the elements can be shifted to fill the hole made by removing an element. For example:

void remove_element(array_type *array, int index, int array_length)
{
   int i;
   for(i = index; i < array_length - 1; i++) array[i] = array[i + 1];
}

Statically allocated arrays can not be resized. Dynamically allocated arrays can be resized with realloc(). This will potentially move the entire array to another location in memory, so all pointers to the array or to its elements will have to be updated. For example:

remove_element(array, index, array_length);  /* First shift the elements, then reallocate */
array_type *tmp = realloc(array, (array_length - 1) * sizeof(array_type) );
if (tmp == NULL && array_length > 1) {
   /* No memory available */
   exit(EXIT_FAILURE);
}
array_length = array_length - 1;
array = tmp;

realloc() will return a NULL pointer if the requested size is 0, or if there is an error. Otherwise, it returns a pointer to the reallocated array. The temporary pointer is used to detect errors when calling realloc() because instead of exiting, it is also possible to just leave the original array as it was. When realloc() fails to reallocate an array, it does not alter the original array.

Note that both of these operations will be fairly slow if the array is large or if a lot of elements are removed. There are other data structures like linked lists and hashes that can be used if efficient insertion and deletion is a priority.

2 of 6
11

What solution you need depends on whether you want your array to retain its order, or not.

Generally, you never only have the array pointer, you also have a variable holding its current logical size, as well as a variable holding its allocated size. I'm also assuming that the removeIndex is within the bounds of the array. With that given, the removal is simple:

Order irrelevant

array[removeIndex] = array[--logicalSize];

That's it. You simply copy the last array element over the element that is to be removed, decrementing the logicalSize of the array in the process.

If removeIndex == logicalSize-1, i.e. the last element is to be removed, this degrades into a self-assignment of that last element, but that is not a problem.

Retaining order

memmove(array + removeIndex, array + removeIndex + 1, (--logicalSize - removeIndex)*sizeof(*array));

A bit more complex, because now we need to call memmove() to perform the shifting of elements, but still a one-liner. Again, this also updates the logicalSize of the array in the process.

๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ c-deleting-an-element-from-array-using-c-123230
Deleting an Element From Array Using C
We check if the user input position is valid or not; it should be between 1 and n. If the position is valid, we place the element left to the position to the place of the deleted element, which is achieved by implementing a loop for moving the element to its left places. Finally, we output the updated array using the printf statement. In this step, we will write a program in C to delete an array element based on the value entered by the user.
๐ŸŒ
Techcrashcourse
techcrashcourse.com โ€บ 2015 โ€บ 03 โ€บ c-program-to-delete-element-from-an-array.html
C Program to Delete an Element from an Array
Here is the 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 deleting an element, the number of elements in array will decrease by one. All elements of array are stored in contiguous memory ...
๐ŸŒ
Simple2Code
simple2code.com โ€บ home โ€บ blog โ€บ c program for deletion of an element from the specified location from array
C Program for deletion of an element from the specified location from Array - Simple2Code
January 7, 2024 - We will specifically learn two different programs: Delete an element by specifying the position. Delete an element by specifying the value. Example: By position. Input Input array elements: 11 22 30 42 50 Specify the position to delete: 3 Output Array elements: 11 22 42 50 ยท Example: By Value. Input Input array elements: 11 22 30 42 50 Specify the value to delete: 22 Output Array elements: 11 30 42 50 ยท Before that, you should have knowledge on the following C topics:
๐ŸŒ
Studytonight
studytonight.com โ€บ c โ€บ programs โ€บ array โ€บ deleting-an-element-from-array
Program to Delete an Element from Array in C | Studytonight
#include<stdio.h> int main() { printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); int array[100], position, c, n; printf("\n\nEnter number of elements in array:"); scanf("%d", &n); printf("\n\nEnter %d elements\n", n); for(c = 0; c < n; c++) scanf("%d", &array[c]); printf("\n\nEnter the location where you want to delete element from: "); scanf("%d", &position); if(position >= n+1) printf("\n\nDeletion not possible\n\n"); else // updating the locations with next elements for(c = position-1; c < n-1; c++) array[c] = array[c+1]; printf("\n\nResultant array is: "); /* the array size gets reduced by 1 after deletion of the element */ for(c = 0; c < n-1; c++) printf("%d ", array[c]); printf("\n\n\t\t\tCoding is Fun !\n\n\n"); return 0; } Below is a simple program to delete an element from array, where the element to be deleted is given by user: