Eliminating duplicates

This is a discussion on Eliminating duplicates within the C Programming forums, part of the General Programming Boards category; Hi everyone...I'm trying to eliminate duplicates from my array...The program compiles find but hangs and will not execute...Could someone look ...

  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,652
    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,670
    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



    ITSA
    Socket Library!

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, 01: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, 10:01 AM
  4. removing duplicates from a linked list
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2003, 09:27 PM
  5. Generating random numbers WITHOUT duplicates
    By fake1 in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 07:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21