I'm trying to find mistakes in my code and i wonder if you could help me. My task is to write a method that would take an array as an input and return this array without left duplicates(last numbers stay). My code doesn't work in case when input is( 1,2,1,2,1,2,3 ) It returns (1, 1, 2, 3) instead of 1,2,3. Here is the code
public static int [] solve(int [] arr){
boolean check=false;
ArrayList<Integer> test = new ArrayList<>(); // idk how to compete this task without ArrayList
for (int i = 0; i < arr.length; i++) { // Here i pass my array to ArrayList
test.add(arr[i]);
}
while(check==false) {
for (int i = 0; i < test.size(); i++) {
for (int j = i + 1; j < test.size(); j++) { // Somewhere here must be my mistake that i can't find
check=true;
if (test.get(i) == test.get(j)) {
test.remove(i);
check=false;
}
}
}
}
// i created second array to return array without duplcates.
int arr2[];
arr2=new int[test.size()];
for(int i=0;i<arr2.length;i++){
arr2[i]=test.get(i);
}
return arr2;
}
}
I tried to complete this task on my own, so i didnt use Google to get help until now. If you know how to improve my code feel free to change everything you want to. Thank you in advance!