Home » Sometech » WAP to remove the duplicate elements from array.

WAP to remove the duplicate elements from array.

#include <stdio.h>

#include<conio.h>

#define SIZE 10

void main()

{

   int i,j,k,n,dup;

    int nums[SIZE];

clrscr();

printf(“Enter how many elements ? “);

scanf(“%d”,&n);

printf(“Enter array elements : \n”);

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

{printf(“nums[%d]=”,i);

scanf(“%d”,&nums[i]);

}

printf(“\nGiven array is:\n “);

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

 printf(” %d”,nums[i]);

 

/*deletion of duplicate */

dup=0;

for(i=0;i<n-1;i++)

{

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

 {

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

   {

    dup=1; //set the flag on

    n=n-1;

        nums[k]=nums[k+1];

     j=j-1;

   }

 }

}

if(dup!=0)

{

  printf(“\nArray after removing \nduplicate elemetns is : \n “);

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

  printf(” %d”,nums[i]);

}

else

 printf(“\nArray is without duplicate elements”);

getch();

}

Advertisements

SoME Tech

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

%d bloggers like this: