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;
}






38 comments:

  1. swetabh malviya10/6/10, 7:03 PM

    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();
    }

    ReplyDelete
    Replies
    1. its easy thnks...

      Delete
    2. i didnt revome more than one duplication

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

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

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

      Delete
  3. who told this doesn't works?it works perfecctly

    ReplyDelete
  4. It works fine no issues.

    ReplyDelete
  5. please anyone can explain this

    ReplyDelete
  6. i cant understand why he took k variable here

    ReplyDelete
  7. #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]);
    }

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

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

    ReplyDelete
  10. very nice program sweatab malvia.............

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. 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();
    }

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

    ReplyDelete
  14. really its a good one awetabh!

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

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

    And Many More....

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

    ReplyDelete
  17. dnxxx dudes.

    ReplyDelete
  18. #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();
    }

    ReplyDelete
  19. 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

    ReplyDelete
  20. 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;
    }

    ReplyDelete
  21. 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)

    ReplyDelete
  22. 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++;
    }
    }

    ReplyDelete
  23. /*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;
    }

    ReplyDelete
  24. 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]);
    }
    }

    ReplyDelete
  25. #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]);
    }

    }

    ReplyDelete
  26. #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]);
    }

    }

    ReplyDelete
  27. 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.

    ReplyDelete
  28. #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();
    }

    ReplyDelete
  29. i want one to help me please :/

    ReplyDelete
  30. Really Good Program easy to understood,thanks.

    ReplyDelete