REMOVE DUPLICATE ELEMENTS IN AN ARRAY USING C PROGRAM





#include<stdio.h>
int main(){
  int arr[50];
  int *p;
  int i,j,k,size,n;
  printf("\nEnter size of the array: ");
  scanf("%d",&n);
  printf("\nEnter %d elements into the array: ",n);
  for(i=0;i<n;i++)
    scanf("%d",&arr[i]);
  size=n;
  p=arr;
  for(i=0;i<size;i++){
    for(j=0;j<size;j++){
         if(i==j){
             continue;
         }
         else if(*(p+i)==*(p+j)){
             k=j;
             size--;
             while(k < size){
                 *(p+k)=*(p+k+1);
                 k++;
              }
              j=0;
          }
      }
  }
  printf("\nThe array after removing duplicates is: ");
  for(i=0;i < size;i++){
    printf(" %d",arr[i]);
  }
  return 0;
}






43 comments:

swetabh malviya said...

this is more easy.....

#include
#include
void main()
{
clrscr();
int a[5];
int n=5,i,j,k;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
for(k=j;k<n;k++)
{
a[k]=a[k+1];
}
n=n-1;
}
}
}
for(i=0;i<n;i++)
printf(" %d ",a[i]);
getch();
}

Anonymous said...

this code doesn't work as a[k+1] is not defined for k=n-1.

Anonymous said...

who told this doesn't works?it works perfecctly

Anonymous said...

It works fine no issues.

Anonymous said...

please anyone can explain this

Anonymous said...

i cant understand why he took k variable here

Unknown said...

#include

void main()
{
int a[20],i,j,m,n;
printf("enter no. of elements: ");
scanf("%d",&n);
printf("\nNow, enter the elements: ");
for(i=0;i1)
for(j=i;j<n-1;j++)
a[j]=a[j+1];
n--;
}
printf("\nThe array after removing duplicates is: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}

Justin said...

I dont think the first program can remove a no repeating 3 times

saurabh said...

can someone give me solution in o(n) or less than o(n2) time.
plz replyyyy

Anonymous said...

very nice program sweatab malvia.............

Unknown said...
This comment has been removed by the author.
Unknown said...

the program below is very good to prevent duplicate entry in an array:

#include
#include
void main()
{
int a[50],i,j=0,n,t;
printf("enter the size=");
scanf("%d",&n);
printf("enter values=");
for(i=0;i=0)
{
if(a[i]==a[j])
{
printf("u have already enter the number=%d\n",a[i]);
printf("enter new value=");
scanf("%d",&a[i]);
}
j=j-1;
}
}
for(i=0;i<n;i++)
{
printf("numbers=%d\n",a[i]);
}
getch();
}

Anonymous said...

swetabh malviya's program worked correctly...thank u

anish said...

really its a good one awetabh!

Anonymous said...

swetabh malviya's Program doesnt work for these test case:

Length : 5 array : 11211
Length : 5 array : 11123

And Many More....

Anonymous said...

But guys,the solutions are in n^2 complexity...

Anonymous said...

dnxxx dudes.

Anand said...

#include
#include
void main()
{
int a[10],i,j,n;
printf("enter the no of elements");
scanf("%d",&n);
printf("enter the array elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("original array");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
j=0;
for(i=1;i<n;i++)
{
if(a[i]!=a[j])
{
j++;
a[j]=a[i];
}
}
n=j+1;
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
getch();
}

pravs said...

one way is to sort the array using best sorting algo.
take a variable that compares each value to its previous value.
Scan array and copy each different element onto a new array
new array would have all duplicate elements removed
time cmplexity:O(nlogn) if used merge/quick/heap sort

Anonymous said...

int arrayUnique(int array[], int size) {
for ( int i = 0; i < size; i++ ) {
for ( int j = i + 1; j < size; j++ ) {
if ( array[i] == array[j] ) {
size -= 1;
for ( int k = j; k < size; k++ ) {
array[k] = array[k+1];
}
j -= 1;
}
}
}
return size;
}

Anonymous said...

To give an answer in less than O(n^2), first sort the array using any known sorting algo(for eg. merge-sort) which requires a time complexity of O(nlogn) and then check every element with the element on its right for duplicity.
So, overall it requires a time complexity of O(nlogn)

Ankit Thakur said...

another simple code for deletion of duplicate elements in an array....

#include
#include
void swap(int a[],int,int);
void main()
{
int n,a[20],i,j;
printf("Input the number of elements...");
scanf("%d",&n);
printf("\nInput the elements...");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Elements before deletion...");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=(i+1);j<n;j++)
{
if(a[i]==a[j])
{
swap(a,j,n);
j=j-1;
n=n-1;
}
}
}
printf("\nElements after deletion...");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
void swap(int a[],int j,int n)
{
while(j<(n-1))
{
a[j]=a[j+1];
j++;
}
}

Anonymous said...

this program does not work for more then one duplicate element..........

Anonymous said...

its easy thnks...

Anonymous said...

i didnt revome more than one duplication

Rahul said...

/*this is for to remove duplicate elements from array*/
#include
int main()
{
int a[8]={11,3,4,3,1,6,3,4};
int n=8,i,j,k;
printf("array element are:");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
a[j]=0;
}
}
}
printf("\nafter deleting duplicate element are:");
for(i=0;i<n;i++)
{
if(a[i]==0)
{
continue;
}
else
{
printf("%d\t",a[i]);
}
}

return 0;
}

Anonymous said...

This one works

#include
void main(){
int a[30],n,h,i,j,k,l,x;
printf("Enter number of elements in the array:");
scanf("%d",&n);
printf("Enter the elements of the array:");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(h=0;h<2;h++){
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]==a[j]){
for(k=j;k<n-1;k++){
a[k]=a[k+1];
}
n=n-1;
}
}
}
}
printf("Now the array is:\n");
for(i=0;i<n;i++){
printf("%d\n",a[i]);
}
}

Anonymous said...

It works!

Anonymous said...

#include
int main()
{
int x[10]={8,3,5,4,4,3,1,8,5,9},n=10,i,k,t,count=0,j;
for(i=0;i0)
{
i--;
n--;

}
}
}
n=n-count;
printf("the array is:");
for(i=0;i<n;i++)
{
printf("%d",x[i]);
}

}

Anonymous said...

can u correct my logic

Anonymous said...

#include
int main()
{
int x[10]={8,3,5,4,4,3,1,8,5,9},n=10,i,k,t,r,count=0,j;
for(i=0;i<n;i++)
{
k=i;
for(j=k+1;k<n;j++)
{
if(x[k]==x[j])
{
x[k]=x[j];
for(t=j+1;t<n;t++)
{
x[j]=x[t];
}
n--;
count++;
}
}
}
r=n-count;
printf("the array is:");
for(i=0;i<n;i++)
{
printf("%d",x[i]);
}

}

Anonymous said...

can u tel me whr is wrng

Sunil Reddy said...

Even swetha's code doesn't work....take input as "1,1,1,1,2,2,3,4,5,5". output is "1,1,2,3,4,5".

It is easy if u take a flag and take an array. Initialize the first element of the new array to the input array and check if elements. If the elements are equal, then make flag as '0', initially flag is '1'. if the element is not equal then insert the element to the new array in the next position and continue. This even requires two for loops. and the condition for second for loop is till the size of the new array.

Anonymous said...

I tried this code did not remove more than one duplicate .please anyone explain

Raiz said...

#include
#include
int main ()
{
int n,a[100], b[100],i,j,count=0;
printf("How many no. do you want to enter ?");
scanf("%d",&n);
printf("Enter %d no.s of the array ",n);
for (i=0;i<n;i++)
scanf("%d",&a[i]);

for(i=0;i<n;i++)
{
for (j=0;j<count;j++)
if (a[i]==b[j]) break;
if (j==count)
{
b[count]=a[i];
count++;
}
}
printf("\nthe array elements after deleting duplicates is :");
for (i=0;i<count;i++)
printf("\n%d",b[i]);
getch();
}

Anonymous said...

helpfull

wafa said...

i want one to help me please :/

Shanthi Ganesan said...

Really Good Program easy to understood,thanks.

Sneh said...

How to replace the duplicate element with 0?

nan said...

i can't understand plz explain

Unknown said...

dont need third inner loop
just 2 loop will remove duplicate
and to remove a duplicate value we need not replace all the values after duplicate
just swap the last value with duplicate and decrease size

void del_duplicate(int *array,int *size){
int i,j,newsize=*size;
for(i=0;i<newsize;i++){
for(j=i+1;j<newsize;j++){

if(array[i]==array[j] && array[newsize-1]!=array[i]){
newsize--;
array[j]=array[newsize];
}else if(array[i]==array[j]){
newsize--;
j=i;
}

}

}
*size = newsize;
}

Unknown said...

dont need third inner loop
just 2 loop will remove duplicate
and to remove a duplicate value we need not replace all the values after duplicate
just swap the last value with duplicate and decrease size

void del_duplicate(int *array,int *size){
int i,j,newsize=*size;
for(i=0;i<newsize;i++){
for(j=i+1;j<newsize;j++){

if(array[i]==array[j] && array[newsize-1]!=array[i]){
newsize--;
array[j]=array[newsize];
}else if(array[i]==array[j]){
newsize--;
j=i;
}

}

}
*size = newsize;
}

Unknown said...

In c program I need remove duplicate array and reverse the array ,size of the removed duplicate array