# Check that no numbers are equal

• 11-25-2006
IdioticCreation
Check that no numbers are equal
Hi,

OK, I have an integer array with 5 numbers in it. How would I check that none of these numbers equal each other. I thought about using an if statement, but it started getting very long and complicated. Does anyone have any ideas about how I could do this.

Thanks,
David
• 11-25-2006
robatino
For a large array the most efficient way is to sort them and then compare adjacent elements. Check out sort() to see if you think it's worthwhile.
• 11-25-2006
IdioticCreation
I think that is what I want, but I am having trouble grasping how to use the augments of sort. They are iterator start, and iterator end. Could you please help me with this?
• 11-25-2006
robatino
Actually, come to think of it, the best way all around with just 5 elements is a double loop. So if you have an array a[5],
Code:

```for (int i=0; i<5; i++) {   for (int j=i+1; j<5; j++) {     if (a[i] == a[j]) {       //equal     }   } }```
Of course for an array of any length N, you could do the same thing. The only problem is that the time is O(N^2). The sorting method gives time O(N log N). Of course, N=5 is small enough it's not worth bringing all the overhead of sort() and it's probably faster to just use the double loop. But if you wanted to do it, it would just look like
Code:

```#include <algorithm> int main() {   int a[5] = {3,1,8,4,2};   std::sort(a, a+5);   for (int i=0; i<4; i++) {  // 4 == 5-1     if (a[i] == a[i+1]) {       // equal     }   } }```
• 11-25-2006
IdioticCreation
Ohh, thanks! I understand the double loop. I still don't understand sort, but it's not important right now.