# 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

2. ### Josh McfarlaneGuest

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

3. ### Pete BeckerGuest

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
4. ### benbenGuest

<> 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
5. ### Guest

What is STL (concerning my question)?

, Jul 19, 2005
6. ### Ken WilsonGuest

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

Ken Wilson
"Coding, coding, over the bounding main()"

Ken Wilson, Jul 19, 2005
7. ### ZorroGuest

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. ### ZorroGuest

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