Thread: duplicate numbers

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    7

    duplicate numbers

    good day to you guys... my first time here...
    i just would like to ask if
    given the series of numbers 8 1 5 9 4 5
    how can i create a program in C to print to screen that there is a duplicate number which is 5...
    thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How do you yourself find if there are duplicate numbers? Well, that's pretty easy. You start with the first number, and look in the whole list for it. Then you pick the next, and so on. Give it a shot.


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

  3. #3
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    If the list tends to be large
    you should sort the list
    and then loop through, checking if successive numbers are equal.

  4. #4
    *this
    Join Date
    Mar 2005
    Posts
    498
    Actually what is better than a sort is something like so...

    Code:
    /* Pseudocode... */
    
    Create an array the size of the frequency of numbers
    For each number in the list
       add one to the spot on the frequency array
    Print the index of each spot with the number 2
    Code:
    // list contains numbers from 1 to 100
    // the frequency array will need to have each number the list has so we must make
    // the last position 100 so we can use 1 instead of 0
    int frequency[100+1] = { 0 };
    
    int size = sizeof (list);
    for (int loop = 0; loop < size; loop++) {
       frequency[ list[loop] ]++;
    }
    
    for (int index = 1; index <= 100; index++) {
       if (frequency[index] == 2) 
          printf("Duplicate number in list: %d", index);
    }
    Last edited by JoshR; 07-15-2005 at 05:27 PM.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    7
    i did it... thank you very much...

  6. #6
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    this looks more correct:

    Code:
    for (i =0; i < sizeof(list) / sizeof(list[0]); i++)
        if (list[i]++)
            printf("duplicate: %d", i);

  7. #7
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    Of course, if the range is small, this method is OK. But if the range is big, for example,
    there could be 32-bit numbers, it will not work: the 'list' array gonna take too much memory.
    In this case, other methods should be used, so numbers must be organized in another
    data structure instead of plain array: linked list, tree, or hash.
    Hashes are particularly useful for checking the uniquesness.
    Quick introduction to hashes can be found at http://silversoft.net/hash.txt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. find and replace duplicate numbers in array
    By Cathalo in forum C++ Programming
    Replies: 5
    Last Post: 02-17-2009, 11:05 AM
  2. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM