Thread: How not to print duplicate numbers in an array?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    27

    How not to print duplicate numbers in an array?

    Question is:
    Use a single-subscripted array to solve the following:
    Read in 20 numbers, each which is between 10 and 100. As each number is read, print it only if it's not a duplicate of a number already read.

    I have done this thing:
    Code:
    #include<stdio.h>
    #define SIZE 20
    
    
    int main()
    {
        int arry[ SIZE ] = 
                { 10, 100, 95, 62, 41, 23, 15, 15, 100, 62,
                 89, 66, 95, 55, 55, 22, 81, 22, 63, 21 };
        int i, pass, hold;
    
    
        /* Bubble Sort */
        for( pass = 1; pass < SIZE; pass++ )
        {
            for( i = 0; i < SIZE - 1; i++ )
            {
                if( arry[ i ] > arry[ i + 1 ] )
                {
                    hold = arry[ i ];
                    arry[ i ] = arry[ i + 1 ];
                    arry[ i + 1 ] = hold;
                }
            }
        }
    
    for( i = 0; i < SIZE; i++)
    	{
    		if( arry[ i ] == arry[ i + 1 ] )
    		{
    			continue;
    		}
    		else
    		{
    			printf( "M", arry[ i ] );
    		}
    	}
    
    
        printf( "\n\n" );
    }
    please help me correct it!
    Last edited by ahmedbatty; 11-16-2011 at 12:46 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    That is not the solution I would use.

    You could try an insertion sort solution like the one you are trying. But, you code is not very well written for me to point out your mistakes.
    I suggest writing a insert function, sort function, and search function and then trying your method again.

    Or, using what I would do which is a array of type bool. Start out with all bools set to false when a number is read in check its bool value.
    If false, set the bool to false and print the value.

    Tim S.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    27
    Quote Originally Posted by stahta01 View Post
    That is not the solution I would use.

    You could try an insertion sort solution like the one you are trying. But, you code is not very well written for me to point out your mistakes.
    I suggest writing a insert function, sort function, and search function and then trying your method again.

    Or, using what I would do which is a array of type bool. Start out with all bools set to false when a number is read in check its bool value.
    If false, set the bool to false and print the value.

    Tim S.

    sorry dude, im just a beginner
    i havent studied any of those functions

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by ahmedbatty View Post
    sorry dude, im just a beginner
    i havent studied any of those functions
    Do you know how to write a function?
    If not, then I see no easy way to solve this problem without writing a mess or using the second solution I suggested.

    Tim S.
    Last edited by stahta01; 11-16-2011 at 01:01 PM.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Your solution works fine... Except you're printing "M" instead of a number for some reason.
    Also, your loop should not check arry[i + 1] if i goes to SIZE -1 because it would check beyond the array limit. But the output looks alright if you correct the print.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-04-2011, 01:16 PM
  2. Print numbers within a certain range of an Array
    By omegasli in forum C Programming
    Replies: 4
    Last Post: 04-10-2011, 01:08 AM
  3. find and replace duplicate numbers in array
    By Cathalo in forum C++ Programming
    Replies: 5
    Last Post: 02-17-2009, 11:05 AM
  4. duplicate numbers
    By derek23 in forum C Programming
    Replies: 6
    Last Post: 07-18-2005, 04:50 AM
  5. Duplicate values in Array
    By TONYMX3 in forum C++ Programming
    Replies: 2
    Last Post: 01-30-2002, 03:57 PM