Velocity Reviews > C++ > How to make sure that all the values in an array are different?

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

simondex@yahoo.com
Guest
Posts: n/a

 07-18-2005
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

Josh Mcfarlane
Guest
Posts: n/a

 07-19-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

Pete Becker
Guest
Posts: n/a

 07-19-2005
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)

benben
Guest
Posts: n/a

 07-19-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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

simondex@yahoo.com
Guest
Posts: n/a

 07-19-2005
What is STL (concerning my question)?

Ken Wilson
Guest
Posts: n/a

 07-19-2005
On 18 Jul 2005 19:51:44 -0700, (E-Mail Removed) 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()"

Zorro
Guest
Posts: n/a

 07-19-2005
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[i] == a[j]) return 0;
else if (a[i] > 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
(E-Mail Removed)
http://www.zhmicro.com
http://distributed-software.blogspot.com

Zorro
Guest
Posts: n/a

 07-19-2005
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[i] == a[j]) return 0;
else if (a[i] > 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
(E-Mail Removed)
http://www.zhmicro.com
http://distributed-software.blogspot.com