Thread: Eliminating duplicates

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    Eliminating duplicates

    Hi everyone...I'm trying to eliminate duplicates from my array...The program compiles find but hangs and will not execute...Could someone look at my loops for eliminating the duplicates and tell me why this is. The program runs fine without the following code, but prints duplicates.

    Code:
    	while(fscanf(fptr,"%d",&array[i]) != NULL)
    	{
    	   for (i = 0; i<MaxElements; i++)
    	   {
    		   for (j = i; j<MaxElements ; j++)
    		   {
    				if ( array[i] == array[j] )
    					
    					for (k=j; k < MaxElements; k++)
    					{
    						array[j] = array[j+1];
    						MaxElements = MaxElements - 1;
    					}
    
    		   }
    	   }
    	   
    	   ++i;
    	   if(i >= MaxElements) break;
    	}
    	--i;
    	fclose(fptr);
    Thanks for any help you can offer.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Don't you want to load the array with numbers before you remove the duplicates?

    Also, your inner-most for loop is looping on 'k', but I don't see 'k' being used.....<hint><hint>

    There are some other things...try this data set when you have something working "1 1 1 1 1 1 1 1 1 1 2 2 2 2 2".


    gg

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    25
    Okay...I changed some things on it and got it a little closer to working properly....It may help more if I display the full program....The file that the program is reading from has the following in it.
    2
    2
    3
    3
    4
    5
    5
    6
    6
    7
    7
    8
    8
    9

    The file that the program writes to after the program is run displays the following.
    5
    3
    2

    which isn't quite right. Here is my updated code.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    FILE *fptr;
    
    int main()
    {
    	int array[15];
    	int i=0;
    	int j=0;
    	int k=0;
    	int MaxElements = 15;
    
    	char readFile[30];
    	char writeFile[30];
    
    	
    	printf("What is the filename that you're going to read from: ");
    	scanf("%s",readFile);
    
    	printf("What is filename that you're going to write to: ");
    	scanf("%s",writeFile);
    
    	//open the file
    	fptr = fopen(readFile,"r"); 
    	if((fptr = fopen(readFile, "r")) == NULL) 
    		exit(1);
    
    	//read input
    
    	while(fscanf(fptr,"%d",&array[i]) != NULL)
    	{
    	   ++i;
    	   if(i >= MaxElements) break;
    
    	}
    
    	--i;
    	fclose(fptr);	   
    
    	for (i = 0; i<MaxElements; i++)
    	   {
    		   for (j = i; j<MaxElements ; j++)
    		   {
    				if ( array[i] == array[j] )
    					
    					for (k=j; k < MaxElements; k++)
    					{
    						array[k] = array[k+1];
    						MaxElements = MaxElements - 1;
    					}
    
    		   }
    	   }
    
    
    	fptr = fopen(writeFile, "w");
    	if(fptr)
    	{
    	   while(i > 0)
    	   {
    		  fprintf(fptr, "%d\n", array[--i]);
    	   }
    	   fclose(fptr);
    	}
    }

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    When you find the duplicate, you will have to remove it by shifting all of the following elements over, right? This could be a potentially slow process. My suggestion would be to have either a second array for copying to, or just set duplicate values to a predetermined value, like -1, to signify an element to be ignored.

    You could even put the two together like this:


    Code:
    // loop thru and mark dup's
    
    int k = 0;
    
    while(k < length) 
    {
     duplicate = array[k++];
       for(int i = k; i < length; ++i)
        if(array[i] == duplicate)
         array[i] = -1; 
    }
    
    k = 0;
    
    // loop thru and erase
    
    for(int i = 0; i < length; ++i)
     if(array[i] != -1) 
      array[k++] = array[i];
    
    length = k; // adjust to new length
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  2. Array with at most n duplicates
    By kratz in forum C++ Programming
    Replies: 19
    Last Post: 07-16-2005, 11:46 PM
  3. Replies: 6
    Last Post: 11-28-2004, 11:01 AM
  4. removing duplicates from a linked list
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2003, 10:27 PM
  5. Generating random numbers WITHOUT duplicates
    By fake1 in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 08:14 PM