Monday, February 15, 2016

C Program to remove duplicate elements from an integer array.

/*=================================================================================
**
**  File Name     : delDuplicate.c
**  Creation Date : Wed 14 Feb 2016 07:38:32 PM IST
**  Last Modified : Mon 15 Feb 2016 07:22:57 AM IST
**  Compoler      : gcc
**  Author        : Manoj Kumar Patra, manojpatra.in@gmail.com
**  Organization  : SCIS, University of Hyderabad, India.
**
**===============================================================================*/

#include<stdio.h>
int main()
{
    printf("\n= = = = = = = = Remove duplicate elements from Array = = = = = = = = =\n");
    int arr[100], i, j, n, k;
    printf("\nHow many elements you wanna insert : ");
    scanf("%d", &n);
    printf("\nInsert the elements in to the array:\n");
    for(i=0; i<n; i++)
    {
        scanf("%d", &arr[i]);
    }
    printf("\nThe elements of the array before deleting duplicate elements:\n");
    for(i=0; i<n; i++)
    {
        printf("%d\t", arr[i]);
    }
    printf("\n-----------------------------------------------------------------------\n");
    for(i=1; i<n; i++)
    {
        //item= arr[i];
        int count=0;
        for(j=i-1; j>=0; j--)
        {
            if(arr[i]==arr[j])
            {
                for(k=i; k<n; k++)
                {
                    arr[k]=arr[k+1];
                }
                n=n-1;
            }
        }
    }
    printf("The elements of the array after deletion of duplicate elements:\n");
    for(i=0; i<n; i++)
    {
        printf("%d\t",arr[i]);
    }
    printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
}

Output:

max@ubuntu:~/cprog$ gcc delDuplicate.c -o delDuplicate
max@ubuntu:~/cprog$ ./delDuplicate

= = = = = = = = Remove duplicate elements from Array = = = = = = = = =

How many elements you wanna insert : 6

Insert the elements in to the array:
2
4
3
4
5
4

The elements of the array before deleting duplicate elements:
2    4    3    4    5    4   
-----------------------------------------------------------------------
The elements of the array after deletion of duplicate elements:
2    4    3    5   
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

No comments:

Post a Comment