I am trying to get my algorithm down to the lowest possible running time.
What is the running time of this algorithm; is it O(log n) or O(n log n) because of the for loop?
import java.util.Arrays;
public class countDifferenceBetweenTwoArrays {
private static int countDifference(int[] arrayA, int[] arrayB) {
int differenceCount = 0;
for (Integer i : arrayA) {
if (Arrays.binarySearch(arrayB, i) < 0) {
differenceCount++;
}
}
for (Integer i : arrayA) {
if (Arrays.binarySearch(arrayB, i) < 0) {
differenceCount++;
}
}
return differenceCount;
}
intandIntegerif you don't need to. Usefor (int i : arrayA)instead, otherwise java needs to autobox theintfor each iteration of the loop.