I have to find duplicate values from an given array. I also have to print the values which are not duplicate, I could not do this part because it gives me an ArrayIndexOutOfBoundException. I caught the exception. Is any performance issue I will face for improper closing or memory leak? My code is below,
import org.apache.commons.lang.ArrayUtils;
public class Duplicates {
public static void main(String[] args) {
Duplicates.duplicate();
}
public static void duplicate() {
// int array[]={1,22,32,66,56,754,22,0,66,55,83,21,67,45,99,666,85,37};
int[] array = { 2, 4, 2, 5, 4, 7, 9, 2, 3, 3 };
int tempArray[] = array;
for (int i = 0; i < array.length - 1; i++) {
for (int j = 1; j < array.length;) {
try {
//this part will print the duplicate values
if ((array[i] == (array[j])) && (i != j) && (i < j)) {
System.out.println("Duplicates values are: " + array[i]);
/* this forloop below is for removing duplicate number from
the entire array and gives a fresh array without
that particular duplicate value */
int removeVal = array[i];
for (int k = i; k < array.length; k++) {
tempArray = ArrayUtils.removeElement(tempArray, removeVal);
}
j = 0;
}
/* This part will print values not duplicate, but need to
print like { 5,7,9 } together */
else {
if (i < j) {
j++;
} else {
j = i + 1;
}
}
array = tempArray;
}
catch (ArrayIndexOutOfBoundsException ae) {
ae.getMessage();
}
}
}
}
}
So what wrong I am doing here that can't print all together and gives ArrayIndexOutOfBoundException? But with the another array(the big one) commented out there it is not giving ArrayIndexOutOfBoundException.