Thread: removing duplicate elements from an array

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    27

    removing duplicate elements from an array

    hello everyone,
    while removing duplicate elements from an array, if more than 4 array elements are same then removal does not work. however my logic seems to be alright. i reckon there is a problem with the conditions and assignments in the three for loops that i have used. the program is as follows:
    Code:
    /*c program to remove duplicate elements in an array*/
    #include<stdio.h>
    int main(void)
    {
         int array[30],i,j,k,n;
         printf("\n How many elements:");
         scanf("%d",&n);
         printf("\n Enter the elements in the array:");
         for(i=1;i<=n;i++)
         {
              scanf("%d",&array[i]);
         }
    	  for(i=1;i<=n-1;i++)/*main loop*/
    	  {
              for(j=i+1;j<=n;j++)/*secondary loop with which the elements of the main loop will be compared*/
                                   /*since the comparison will be between ith and the (i+1)th element thatswhy j=i+1*/   
              {
                    if(array[i]==array[j])/*if any element of the main loop is found to be equal to any element of the secondary loop*/ 
                    {
                         for(k=j;k<=n;k++)/*loop to shift the elements of the array one place leftwards to take the place of the 
                                            removed duplicate element*/
                         {
                               array[k]=array[k+1];/*the assignment to facilitate the newly arranged array*/
                         }
                         i=1;/*once the array has been rearranged main loop rebegins to check duplicacy in
                               case there are more than 1 duplicate elements or more than 1 instance of the same element*/ 
                         n--;/*for each removal of any duplicate element the number of total elements in array decreases by 1*/
                    }
              }
         }
         printf("\n Array after removing duplicate elements is:");
         for(i=1;i<=n;i++)
         {
              printf("\t %d",array[i]);/*printing the pruned array*/
         }
         return 0;
    }
    take for instance if the input elements are 1,1,1,1,1,2,2,3,5 then after removal of duplicacy the array is 1,1,2,3,5. please help!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Say you compare i=1 and j=2 and find they are the same. You move things over to the left to crunch the duplicate; but the next pass goes with i=1 and j=3. However, i=1 has not been checked against the *new* j=2....

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Don't set i back to 1, just let it be.
    However, you do need to subtract 1 from j to allow the same j-indexed element to be checked again since it has just been replaced by the next element. (Alternatively, you could remove the j++ from the for statement, put it just before the ending brace of that loop, and if the elements are equal, use a continue statement to jump back to the beginning of the loop without incrementing j.)

    BTW, arrays in C are usually indexed from 0 to n - 1 for an array of size n.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Another approach to remove duplicates would be this:

    1. sort the array some defined order (numerically, etc.)
    2. go through the sorted array and skip the elements which are repetitions of each other

    In a unix pipeline it translates to this

    Code:
    cat data | sort | uniq

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Removing elements from an array of linked list
    By koplersky in forum C Programming
    Replies: 13
    Last Post: 10-08-2012, 02:04 PM
  2. removing a duplicate entry
    By csharp100 in forum C Programming
    Replies: 4
    Last Post: 04-20-2012, 10:02 AM
  3. Please help with Printing duplicate array elements
    By TheSprawl in forum C Programming
    Replies: 9
    Last Post: 11-23-2011, 11:22 PM
  4. Removing elements from an array
    By monki000 in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2010, 12:23 PM
  5. Newbie needs help removing elements from an array
    By daki76 in forum C++ Programming
    Replies: 10
    Last Post: 06-20-2006, 09:42 AM

Tags for this Thread