By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

duplicates

P: 22
how to u remove duplicates from an array of integers
Mar 15 '07 #1
Share this Question
Share on Google+
4 Replies


dmjpro
100+
P: 2,476
i think u already captured how to populate array .....

look at this code carefully .....

Expand|Select|Wrap|Line Numbers
  1. int a[] = {x,y,.......10th};
  2. int count = 10;
  3.  
  4. for(int i = 0;i<count;i++)
  5. {
  6.    for(int j=count-1;j>i;j++) //Check duplicate
  7.    {
  8.       if(a[j] == a[i]) //Duplicate found
  9.       {
  10.          for(int k=i;k<count-1;k++)
  11.          {
  12.             a[k]=a[k+1];
  13.             count++;
  14.          }
  15.       }
  16.    }
  17. }
with out real test i send u the code plz verify it and contact with me ......

best of luck
Mar 15 '07 #2

sicarie
Expert Mod 2.5K+
P: 4,677
dmjpro-

after running your code, my compiler threw a segfault, I'm pretty sure it's because of your j++ at the end of the second for loop.

holla-

I would recommend going through and finding out how many duplicates there are

pseudocode:
take the first compare it to all the rest
if it's a duplicate,
increment duplicateCounter
take the second and compare it to the third and on (it's already been comapred to the first)
if it's a duplicate,
increment duplicateCounter
take the third and compare it to the fourth and on
....
subtract duplicateCounter from ten
create an array with that many places
populate everything but the duplicates

You could always also just create an array of the same size, fill it with values that you know won't ever occur (if you do know what values will occur) like -1, and then just copy anything that's not a duplicate - that removes the whole process of finding the size of the new array, and then you could just print it out until you hit that value... but that's only if you know that you're not going to ever have an element with that number.
Mar 15 '07 #3

dmjpro
100+
P: 2,476
sooryy... my mistake
Mar 15 '07 #4

P: 5
Hey bro, here's some code I just wrote to illustrate.
The code is not fancy as I tried to make it very 'step-by-step' mode and wrote it quickly.
There is a single comment on it.
If you need further comments on it to help you understand, please mail me.

Kind regards,

Fabiano.
//----------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. #define ARRAYMAXSIZE 10
  5.  
  6. int main(void)
  7. {
  8.     int array1[ARRAYMAXSIZE] = {5, 4, 1, 3, 2, 4, 3, 2, 5, 5},
  9.         array2[ARRAYMAXSIZE] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  10.         count1 = 0, count2 = 0, alreadyexists = 0, *frompointer, *topointer, *checkpointer;
  11.  
  12.     clrscr();
  13.         printf("\n\n\n");
  14.  
  15.     frompointer = &array1[0];
  16.     topointer = &array2[0];
  17.     checkpointer = &array2[0];
  18.  
  19.     for(count2 = 0; count2 < ARRAYMAXSIZE; count2++, frompointer++)
  20.     {
  21.         checkpointer = &array2[0];
  22.         alreadyexists = 0;
  23.         for(count1 = 0; count1 < ARRAYMAXSIZE; count1++, checkpointer++)
  24.         {
  25.             if(*frompointer == *checkpointer)
  26.             {
  27.                 alreadyexists = 1;
  28.                 break; // no sense in continue checking for duplicates if you already found one
  29.             }
  30.         }
  31.         if(!alreadyexists)
  32.         {
  33.             *topointer = *frompointer;
  34.             topointer++;
  35.         }
  36.     }
  37.     for(count1 = 0; count1 < ARRAYMAXSIZE; count1++)
  38.     {
  39.         if(array2[count1] > 0)
  40.             printf("%d ", array2[count1]);
  41.     }
  42.     return 0;
  43. }
  44.  
Mar 15 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.