different ints in array

I have an int myArray[4], and I want to insert a little code to check and make sure that all four items in the array are different integers.

I want to avoid this big long if statement, if I can:

[code=c++]
if(myArray[0] != myArray[1] && myArray[0] != myArray[2] && myArray[0] != myArray[3] && myArray[1] != myArray[2] && myArray[1] != myArray[3] && myArray[2] != myArray[3])
{
cout << myArray;
}
else
{
}
[/code]

It's ok for int myArray[4], but if I have int myArray[8] it becomes really cumbersome.

Any ideas?

Thank you!
You will need a loop. Do it for three, four and five by hand first, the algorithm should be clear by then.
After the array is created:

1
2
3
#include <set> 
if( std::set<int>( myArray, myArray + 4 ).size() != 4 ) 
    cout << "There was a duplicate" << endl;


Or while you are adding elements to the array:
1
2
3
#include <algorithm>  // for std::find
if( std::find( myArray, myArray + 4, intToAdd ) != myArray + 4 )
    cout << "About to insert the duplicate value " << intToAdd << endl;


Topic archived. No new replies allowed.