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
}
}
}