0
0
CProgramBeginner · 2 min read

C Program to Delete Element from Array with Output

To delete an element from an array in C, find the element's index, then shift all elements after it one position left using a loop, and reduce the array size by one; for example, use for (int i = pos; i < n - 1; i++) arr[i] = arr[i + 1]; n--;.
📋

Examples

InputArray: [10, 20, 30, 40, 50], Delete: 30
OutputArray after deletion: 10 20 40 50
InputArray: [1, 2, 3, 4, 5], Delete: 1
OutputArray after deletion: 2 3 4 5
InputArray: [5, 10, 15], Delete: 100
OutputElement not found in the array.
🧠

How to Think About It

First, find the position of the element to delete by checking each item in the array. If the element is found, move every element after it one step to the left to overwrite it. Finally, reduce the count of elements by one to reflect the deletion.
📐

Algorithm

1
Get the size of the array and the element to delete.
2
Search the array for the element's position.
3
If element not found, print message and stop.
4
If found, shift all elements after it one position left.
5
Decrease the array size by one.
6
Print the updated array.
💻

Code

c
#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;
}
Output
Enter number of elements: 5 Enter elements: 10 20 30 40 50 Enter element to delete: 30 Array after deletion: 10 20 40 50
🔍

Dry Run

Let's trace deleting 30 from the array [10, 20, 30, 40, 50].

1

Input array and element

Array: [10, 20, 30, 40, 50], Element to delete: 30

2

Find element position

Check each element: 10 != 30, 20 != 30, 30 == 30 at index 2

3

Shift elements left

Move 40 to index 2, 50 to index 3

4

Reduce array size

New size is 4

5

Print updated array

Array after deletion: 10 20 40 50

IndexValue BeforeValue After
23040
34050
450-
💡

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

Mark and skip approach
c
#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;
}
This method prints the array without the element but does not modify the original array or its size.
Using a new array to copy elements
c
#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;
}
This approach uses extra space to create a new array without the element, preserving the original array.

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.

ApproachTimeSpaceBest For
In-place shiftingO(n)O(1)Efficient memory use and speed
Mark and skip printingO(n)O(1)Quick output without modifying array
Copy to new arrayO(n)O(n)Preserving original array
💡
Always check if the element exists before shifting to avoid errors.
⚠️
Beginners often forget to reduce the array size after deleting the element, causing incorrect output.