Thread: Can you give me your tip plz :)

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    35

    Can you give me your tip plz :)

    How i can find duplicated numbers in an array.

    #1.I have a rand() function that assign numbers in an array.

    #2.I want to find if there are at least 2 duplicated numbers,
    unfortunately i dont know how ... can you give me your suggestions plz...

  2. #2
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Quote Originally Posted by dionys
    How i can find duplicated numbers in an array.

    #1.I have a rand() function that assign numbers in an array.

    #2.I want to find if there are at least 2 duplicated numbers,
    unfortunately i dont know how ... can you give me your suggestions plz...
    Just check each number in the array against the others...so it'd be like:

    Code:
    i = 1;
    t = 20;
    
    while i < 20
      if numbers_in_array[i] = numbers_in_array[i++]
         do something
    I know this doesn't work, and there is flaws in it, but it's a general idea.
    Last edited by Quantrizi; 04-11-2004 at 05:08 PM. Reason: added warning

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >can you give me your suggestions plz...
    Use a frequency table would be my first suggestion:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int a[] = {0,1,2,3,4,5,4,6,7,3,8,9};
      int freq[10] = {0};
      int i;
    
      for ( i = 0; i < 12; i++ )
        freq[ a[i] ]++;
      for ( i = 0; i < 10; i++ )
        printf ( "%d: %d\n", i, freq[i] );
    
      return 0;
    }
    My best code is written with the delete key.

  4. #4
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    2 nested loops, try comparing each array element with every other array element. give it a shot, post code, and the highly secret, arcane knowledge required to do this (which cannot be found anywhere else on the internet!) will be passed on to you, young padawan

    edit: o shi
    .sect signature

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Prelude's idea of using a frequency table is definitely more efficient than checking every element in an array in most cases, although if you're choosing 10 random numbers from a group of 400 000, the check every element method works much better (in terms of memory efficiency).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >although if you're choosing 10 random numbers from a group of 400 000
    Of course, since the OP gave no specific numbers, we can't make that assumption. So without more detail, the simpler method is preferred. If the range of numbers is too much for a table, then a binary search tree would be much better than a table for space usage, without much loss in performance. However, the nested loop option will likely be more efficient if the arrays are sure to be very small. The choice of solution depends on the data.
    My best code is written with the delete key.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by Prelude
    >although if you're choosing 10 random numbers from a group of 400 000
    Of course, since the OP gave no specific numbers, we can't make that assumption. So without more detail, the simpler method is preferred.
    Or the guaranteed method is preferred, as a freq table may be too large and unwieldly...

    Quote Originally Posted by Prelude
    The choice of solution depends on the data.
    So true
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Plz give some comments over this topic
    By 03005654926 in forum C++ Programming
    Replies: 5
    Last Post: 06-18-2009, 10:35 AM
  2. plz help me...
    By sweetchakri in forum C Programming
    Replies: 1
    Last Post: 03-03-2009, 11:50 PM
  3. Please Give Me what i want..
    By chottachatri in forum C++ Programming
    Replies: 5
    Last Post: 03-24-2008, 04:11 AM
  4. Displaying a balloon tip on sys trap app at startup
    By BobS0327 in forum Windows Programming
    Replies: 0
    Last Post: 03-25-2005, 08:35 PM
  5. How To Give A Font Colour ?
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 09-14-2001, 01:22 PM