I'm given an array of doubles, all numbers are equal except for one. My task is to find that unique number. My code returns the correct output and now I'm wondering how will I further optimize it.
This is my code:
public static double findUnique(double array[]) {
double unique = 0;
double common = 0;
for(int i=0; i<array.length; i++) {
for(int j=i+1; j<array.length; j++) {
if(array[i]==array[j]) {
common = array[i];
}
}
if(common!=array[i]) {
unique = array[i];
}
}
return unique;
}
The only thing I could think of is storing the length of the array first, but after some testing it actually took longer. Thanks.