C Program to Delete Element from Array with Output
for (int i = pos; i < n - 1; i++) arr[i] = arr[i + 1]; n--;.Examples
How to Think About It
Algorithm
Code
#include <stdio.h> int main() { int arr[100], n, i, pos = -1, val; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter elements:\n"); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } printf("Enter element to delete: "); scanf("%d", &val); for (i = 0; i < n; i++) { if (arr[i] == val) { pos = i; break; } } if (pos == -1) { printf("Element not found in the array.\n"); } else { for (i = pos; i < n - 1; i++) { arr[i] = arr[i + 1]; } n--; printf("Array after deletion: "); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); } return 0; }
Dry Run
Let's trace deleting 30 from the array [10, 20, 30, 40, 50].
Input array and element
Array: [10, 20, 30, 40, 50], Element to delete: 30
Find element position
Check each element: 10 != 30, 20 != 30, 30 == 30 at index 2
Shift elements left
Move 40 to index 2, 50 to index 3
Reduce array size
New size is 4
Print updated array
Array after deletion: 10 20 40 50
| Index | Value Before | Value After |
|---|---|---|
| 2 | 30 | 40 |
| 3 | 40 | 50 |
| 4 | 50 | - |
Why This Works
Step 1: Find element index
We use a loop to find the position of the element to delete by comparing each array item with the target value.
Step 2: Shift elements
Once found, we overwrite the element by moving all subsequent elements one position to the left using a loop.
Step 3: Update size and print
We reduce the array size by one to reflect the deletion and print the updated array to show the result.
Alternative Approaches
#include <stdio.h> int main() { int arr[100], n, i, val, count = 0; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter elements:\n"); for (i = 0; i < n; i++) scanf("%d", &arr[i]); printf("Enter element to delete: "); scanf("%d", &val); printf("Array after deletion: "); for (i = 0; i < n; i++) { if (arr[i] != val) { printf("%d ", arr[i]); count++; } } if (count == n) printf("\nElement not found in the array.\n"); else printf("\n"); return 0; }
#include <stdio.h> int main() { int arr[100], newArr[100], n, i, j = 0, val; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter elements:\n"); for (i = 0; i < n; i++) scanf("%d", &arr[i]); printf("Enter element to delete: "); scanf("%d", &val); for (i = 0; i < n; i++) { if (arr[i] != val) { newArr[j++] = arr[i]; } } if (j == n) { printf("Element not found in the array.\n"); } else { printf("Array after deletion: "); for (i = 0; i < j; i++) printf("%d ", newArr[i]); printf("\n"); } return 0; }
Complexity: O(n) time, O(1) space
Time Complexity
The program loops through the array once to find the element and then shifts elements, both operations combined take linear time O(n).
Space Complexity
The deletion is done in-place by shifting elements, so no extra significant memory is used, resulting in O(1) space.
Which Approach is Fastest?
The in-place shifting method is fastest and uses least memory compared to copying to a new array or printing while skipping.
| Approach | Time | Space | Best For |
|---|---|---|---|
| In-place shifting | O(n) | O(1) | Efficient memory use and speed |
| Mark and skip printing | O(n) | O(1) | Quick output without modifying array |
| Copy to new array | O(n) | O(n) | Preserving original array |