C Program to Remove Duplicate Elements From a Sorted Array - Source Code, Output & Execution Flow

C Program to Remove Duplicate Elements From a Sorted Array

This tutorial explains c program to remove duplicate elements from a sorted array with C source code, sample output, and step-by-step execution flow.

#include <stdio.h>
int main() {
    int a[] = {1, 1, 2, 2, 3, 4, 4};
    int n = 7, i, j = 0;
    for (i = 0; i < n - 1; i++) {
        if (a[i] != a[i + 1]) a[j++] = a[i];
    }
    a[j++] = a[n - 1];
    for (i = 0; i < j; i++) printf("%d ", a[i]);
    return 0;
}
1 2 3 4

In this program, the input is read first when required, the core logic is applied step by step, and the final result is displayed using printf(). Follow the execution flow beside the code to understand how each statement contributes to the output.