Search This Blog

Thursday, 31 October 2024

7. Write a C Program to remove Duplicate Element in a single dimensional Array

 /* 7. Write a C Program to remove Duplicate Element in a single dimensional Array */

#include <stdio.h>

void main()

{

    int arr[20];     // Declare an array of size 20

    int size;

    int i, j, k;      // Loop control variables

    clrscr();

    printf("Enter size of the array:");

    scanf("%d", &size);  // Read size of the array

    printf("Enter elements in array:\n");

    for(i=0; i<size; i++)  // Read array elements

    {

        scanf("%d", &arr[i]);

    }

     //Find duplicate elements in the array

    for(i=0; i<size; i++)

    {

     for(j=i+1; j<size; j++)

     {

         // Check for duplicate elements

         if(arr[i] == arr[j])

         {

           // Delete the current duplicate element

                for(k=j; k < size - 1; k++)

                {

                    arr[k] = arr[k + 1];

                }

           // Decrement size after removing duplicate element

                size--;

                j--;

            }

        }

    }

     //Display array after deleting duplicate elements

    printf("\nArray elements after deleting duplicates: ");

    for(i=0; i<size; i++)

    {

     printf("%d\t", arr[i]);

    }

     getch();    

}

No comments :

Post a Comment

FREE Hit Counters