C

C Program to Remove Duplicate Elements from an Array: A Step-by-Step Guide

Write C Program to Remove Duplicate Elements from Array

#include <stdio.h>

int removeDuplicates(int arr[], int n) {
    // Return if array is empty or has only one element
    if (n == 0 || n == 1)
        return n;

    int temp[n]; // Temporary array to store unique elements
    int j = 0;   // Index for the temporary array

    // Traverse the original array
    for (int i = 0; i < n - 1; i++) {
        // If current element is not equal to the next element, store it in temp[]
        if (arr[i] != arr[i + 1]) {
            temp[j++] = arr[i];
        }
    }

    // Store the last element (since it's not checked in the loop)
    temp[j++] = arr[n - 1];

    // Copy unique elements back to the original array
    for (int i = 0; i < j; i++) {
        arr[i] = temp[i];
    }

    return j; // Return the new size of the array without duplicates
}

int main() {
    int arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("Original array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    // Remove duplicates
    n = removeDuplicates(arr, n);

    printf("Array after removing duplicates: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}
Original array: 1 2 2 3 4 4 4 5 5 
Array after removing duplicates: 1 2 3 4 5 

Logic and Explanation

  1. Input Array:
    The program starts with an array of integers, which may contain duplicate elements. For example: {1, 2, 2, 3, 4, 4, 4, 5, 5}.
  2. Temporary Array:
    A temporary array temp[] is created to store unique elements. This array will eventually replace the original array after duplicates are removed.
  3. Traverse the Original Array:
    The program iterates through the original array using a for loop. It compares each element with the next one:
    • If the current element is not equal to the next element, it is added to the temp[] array.
    • If the current element is equal to the next element, it is skipped (as it’s a duplicate).
  4. Store the Last Element:
    The last element of the array is always unique (since it’s not compared to any next element), so it is added to temp[] after the loop.
  5. Copy Back to Original Array:
    The unique elements from temp[] are copied back to the original array.
  6. Return the New Size:
    The function returns the new size of the array (without duplicates), which is used to print the updated array.

Why This Logic Works

  • The program assumes the input array is sorted. If the array is not sorted, you need to sort it first (using qsort() or another sorting algorithm).
  • By comparing adjacent elements, duplicates are easily identified and skipped.
  • The use of a temporary array ensures that the original array is not modified until all unique elements are identified.

gp

Are you looking to learn a programming language but feeling overwhelmed by the complexity? Our programming language guide provides an easy-to-understand, step-by-step approach to mastering programming.

Leave a Reply

Your email address will not be published. Required fields are marked *