I want to compare each item in an array to every other item in said array. I’m doing this at the moment, is it ok or is there a prettier/faster/more logical way of doing it?
for(int i=0; i<array1.size(); i++){
for(int j=0; j<array1.size(); j++){
if(i!=j){
..do stuff..
}
}
}
You’re doing too many comparisons, plus you’re comparing every item to itself needlessly. What you want is this:
(This is assuming that your idea of equality is commutative, which equality normally is.)
If there are N items in your array, your original snippet would be doing N^2 comparisons, whereas my snippet is doing N(N-1)/2 comparisons.