Let A be an array of size n >= 6 containing integers form 1 to n - 5,inclusive, with exactly five repeated. Describe a good algorithm for finding the 5 integers in A that are repeated.
public class findDuplicate {
public static int[] findDup(int A[]){
int B[] = new int[A.length];
int dup[] = new int[5];
int index = 0;
for(int i = 0; i < B.length; i++){
B[i] = 0;
}
for(int j = 0; j < A.length; j++){
if(B[A[j]] == 0){
B[A[j]] = 1;
}else{
dup[index++] = A[j];
}
}
return dup;
}
public static void main(String args[]){
int[] A = {1,2,3,1,4,3,6,5,5,7,8,7,10,9,11,9,13,12,14,15};
int[] dup = findDup(A);
printArray.printArrayint(dup);
}
}