I had an interview today which involved this very question and in order to widen my algorithmic knowledge. I am trying to see if there are any better suggestions.
I was trying to find duplicates in an array without using java.util and widen my algorithmical knowledge in regards to addressing space and time complexities.
Below is the code I produced during the technical assessment:
public static boolean isThereDuplicates(int[] A){
for (int i = 0; i < A.length; i++)
for (int j = i + 1; j < A.length; j++){
if (A[i] == A[j])
return true;
}
return false;
}
This simple algorithm looks identical to the Bubble Sort, which runs in O(N^2). Is there any other better algorithms that I could use to achieve this?