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

S

simondex

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
 
J

Josh Mcfarlane

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.
 
P

Pete Becker

Josh said:
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.
 
B

benben

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
 
K

Ken Wilson

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()"
 
Z

Zorro

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
(e-mail address removed)
http://www.zhmicro.com
http://distributed-software.blogspot.com
 
Z

Zorro

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
(e-mail address removed)
http://www.zhmicro.com
http://distributed-software.blogspot.com
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top