duplicates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • holla
    New Member
    • Mar 2007
    • 22

    duplicates

    how to u remove duplicates from an array of integers
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    i think u already captured how to populate array .....

    look at this code carefully .....

    Code:
    int a[] = {x,y,.......10th};
    int count = 10;
    
    for(int i = 0;i<count;i++)
    {
       for(int j=count-1;j>i;j++) //Check duplicate
       {
          if(a[j] == a[i]) //Duplicate found
          {
             for(int k=i;k<count-1;k++)
             {
                a[k]=a[k+1];
                count++;
             }
          }
       }
    }
    with out real test i send u the code plz verify it and contact with me ......

    best of luck
    Last edited by Ganon11; Mar 15 '07, 05:32 PM. Reason: code tags added, indented

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      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 duplicateCounte r
      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 duplicateCounte r
      take the third and compare it to the fourth and on
      ....
      subtract duplicateCounte r 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.

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        sooryy... my mistake

        Comment

        • itgendevelopment
          New Member
          • Mar 2007
          • 5

          #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.
          //----------------------------------------------------
          Code:
          #include <stdio.h>
          #include <conio.h>
          
          #define ARRAYMAXSIZE 10
          
          int main(void)
          {
          	int array1[ARRAYMAXSIZE] = {5, 4, 1, 3, 2, 4, 3, 2, 5, 5},
          		array2[ARRAYMAXSIZE] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
          		count1 = 0, count2 = 0, alreadyexists = 0, *frompointer, *topointer, *checkpointer;
          
          	clrscr();
                  printf("\n\n\n");
          
          	frompointer = &array1[0];
          	topointer = &array2[0];
          	checkpointer = &array2[0];
          
          	for(count2 = 0; count2 < ARRAYMAXSIZE; count2++, frompointer++)
          	{
          		checkpointer = &array2[0];
          		alreadyexists = 0;
          		for(count1 = 0; count1 < ARRAYMAXSIZE; count1++, checkpointer++)
          		{
          			if(*frompointer == *checkpointer)
          			{
          				alreadyexists = 1;
          				break; // no sense in continue checking for duplicates if you already found one
          			}
          		}
          		if(!alreadyexists)
          		{
          			*topointer = *frompointer;
          			topointer++;
          		}
          	}
          	for(count1 = 0; count1 < ARRAYMAXSIZE; count1++)
          	{
          		if(array2[count1] > 0)
          			printf("%d ", array2[count1]);
          	}
          	return 0;
          }

          Comment

          Working...