How to make sure that all the values in an array are different?

Discussion in 'C++' started by simondex@yahoo.com, Jul 19, 2005.

  1. Guest

    Hi, Everyone!

    How could I make sure that nine values (from 0 to N) are all different
    without writing too many loops? I am not afraid of statistical
    parameters (e.g. coefficient of variation, etc.)

    Thank You Very Much.

    Truly Yours, Simon Dexter
    , Jul 19, 2005
    #1
    1. Advertising

  2. wrote:
    > Hi, Everyone!
    >
    > How could I make sure that nine values (from 0 to N) are all different
    > without writing too many loops? I am not afraid of statistical
    > parameters (e.g. coefficient of variation, etc.)
    >
    > Thank You Very Much.
    >
    > Truly Yours, Simon Dexter


    What's wrong with loops? With only 9 values you wouldn't be taking that
    much of a performance hit to start with the first value, compare it to
    the remaining 8, then the 2nd value, remaining 7, etc.

    If that's not acceptable though, I'm sure you can find some built in
    algorithms to test for uniqueness of values.

    unique seems like a good STL candidate, however, you'd have to have
    your values in a sorted container in order to use it.
    Josh Mcfarlane, Jul 19, 2005
    #2
    1. Advertising

  3. Pete Becker Guest

    Josh Mcfarlane wrote:
    >
    > unique seems like a good STL candidate, however, you'd have to have
    > your values in a sorted container in order to use it.
    >


    A sorted container isn't needed. All that's needed is a sequence that's
    sorted.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, Jul 19, 2005
    #3
  4. benben Guest

    <> wrote in message
    news:...
    > Hi, Everyone!
    >
    > How could I make sure that nine values (from 0 to N) are all different
    > without writing too many loops? I am not afraid of statistical
    > parameters (e.g. coefficient of variation, etc.)
    >
    > Thank You Very Much.
    >
    > Truly Yours, Simon Dexter
    >


    Use a set
    benben, Jul 19, 2005
    #4
  5. Guest

    What is STL (concerning my question)?
    , Jul 19, 2005
    #5
  6. Ken Wilson Guest

    On 18 Jul 2005 19:51:44 -0700, did courageously
    avow:

    >What is STL (concerning my question)?


    It is the C++ Standard Template Library the compile vendor supplies
    with your compiler. It is the place for generic everything,
    containers, iterators for containers, algorithms. If you're asking
    that question be ready for some heady stuff.

    Ken Wilson
    "Coding, coding, over the bounding main()"
    Ken Wilson, Jul 19, 2005
    #6
  7. Zorro Guest

    Actually, it is only two loops, and the complexity is n*n. Write a
    function that receives your array. If you do not want to to change the
    array, make a local one and copy your array to it.

    Now, do a selection sort, but return at any time when the test for
    equality of items in the array is true.

    Suppose you return 0 on equality, otherwise return 1 at end of function
    when sort completes, meaning all elements are different.

    The test could look like this:

    if (a == a[j]) return 0;
    else if (a > a[j]) do-selection-sort

    and if you never returned from inside the loops (the equality), now
    return 1.

    Hope it helps.

    Regards,
    Dr. Z.
    Chief Scientist

    http://www.zhmicro.com
    http://distributed-software.blogspot.com
    Zorro, Jul 19, 2005
    #7
  8. Zorro Guest

    Actually, it is only two loops, and the complexity is n*n. Write a
    function that receives your array. If you do not want to to change the
    array, make a local one and copy your array to it.

    Now, do a selection sort, but return at any time when the test for
    equality of items in the array is true.

    Suppose you return 0 on equality, otherwise return 1 at end of function
    when sort completes, meaning all elements are different.

    The test could look like this:

    if (a == a[j]) return 0;
    else if (a > a[j]) do-selection-sort

    and if you never returned from inside the loops (the equality), now
    return 1.

    Hope it helps.

    Regards,
    Dr. Z.
    Chief Scientist

    http://www.zhmicro.com
    http://distributed-software.blogspot.com
    Zorro, Jul 19, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ryan Stewart
    Replies:
    3
    Views:
    625
    xarax
    Sep 7, 2003
  2. Replies:
    0
    Views:
    305
  3. Replies:
    0
    Views:
    544
  4. PerlFAQ Server
    Replies:
    4
    Views:
    123
    brian d foy
    Jan 3, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    144
    PerlFAQ Server
    Mar 30, 2011
Loading...

Share This Page