Removing Duplicates from an Array

This is a discussion on Removing Duplicates from an Array within the C Programming forums, part of the General Programming Boards category; Hi, I have a homework assignment for my programming course that I'm having a lot of trouble with. The object ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    14

    Unhappy Removing Duplicates from an Array

    Hi, I have a homework assignment for my programming course that I'm having a lot of trouble with. The object is to take an array, and remove all of the duplicate numbers, and create another array without the dupes. Here's my code right now:

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include "simpio.h"
    #define size 10
    
    int removedup(int inpArray[],int arraySize, int outArray[]);
    
    main()
    {
    	int inpArray[size],outArray[size],i;
    	printf("Enter integer values for the array");
    	for(i=0;i<size;i++)
    		inpArray[i]=GetInteger();
    	i=removedup(inpArray,10,outArray);
    	printf("%d\n",i);
    	for(i=0;i<size;i++)
    		printf("%d\t",inpArray[i]);
    	for(i=0;i<size;i++)
    		printf("%d\t",outArray[i]);
    }
    
    int removedup(int inpArray[],int arraySize, int outArray[])
    {
    	int i,temp,j,k;
    	k=0;
    	for (i=0;i<size;i++)
    	{
    		
    		temp=inpArray[i];
    		for (j=0;j<size;j++)
    		{
    			if (outArray[j]==inpArray[i]) 
    			{
    				b	break;
    			}
    			else outArray[k]=inpArray[i];
    			k++;
    		}
    		
    	}
    	return k;
    }
    The function removedup is supposed to return the size that the second array (The one without duplicates) should be, but when I run it, I always get an output of either 10, or the program terminates after the 9th cycle of i. Thanks in advance for any help.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You just need to think about how you'd do it.
    Code:
    start copying values from the first array to the second
        check each element of the new array, up to the number of items you have in it
            if it is already in there, stop looking for the number
        if you have reached the end of the new array, the end being the count of elements
            add the value from the first array here, and increment the count
    You can simplify this a bit, but that'll get you started. You need to keep track of the number of items you have in the second array, which will start at zero, and only look through that number of items in the array.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,846
    > i=removedup(inpArray,10,outArray);
    1. You pass a parameter 10, rather than size
    2. You assign the result to the same variable you then trash as your loop variable

    Say perhaps
    Code:
    	newSize=removedup(inpArray,size,outArray);
    	for(i=0;i<size;i++)
    		printf("%d\t",inpArray[i]);
    	for(i=0;i<newSize;i++)
    		printf("%d\t",outArray[i]);
    > int removedup(int inpArray[],int arraySize, int outArray[])
    You don't use the parameter size at all, but just the macro value.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    14
    Thanks for the help, but there seems to be something wrong with my actual function to check for duplicates. Here's what I'm using, please tell me if you see the flaw, and thanks again.

    Code:
    int removedup(int inpArray[],int arraySize, int outArray[])
    {
    	int i,newSize,j,temp;
    	for (i=0;i<size;i++)
    	{
    		outArray[i]=0;
    	}
    	newSize=0;
    	for (i=0;i<size;i++)
    	{
    		temp=inpArray[i];
    		for(j=0;j<=i;j++)
    		{
    			if (inpArray[i]==outArray[j]) break;
    			else
    			{
    				outArray[newSize]=temp;
    				newSize++;
    			}
    		}
    	}
    	return (newSize);
    }

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You need to follow the logic of your code, and see if it is doing what you think it is.
    Put a few printf() statements in there to output some debug style information to help yourself. Work out where each loop starts, and what makes it terminate.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  2. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-12-2004, 12:15 AM
  3. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 05:27 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM
  5. Replies: 2
    Last Post: 06-18-2002, 12:03 PM

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