Remove duplicates in array with a condition Java
Remove duplicates in array with a condition Java
I have a task, to remove duplicates in array, what by "remove" means to shift elements down by 1
, and making the last element equal to 0
,
so if I have int array = {1, 1, 2, 2, 3, 2};
output should be like:1, 2, 3, 0, 0, 0
I tried this logic:
1
0
int array = {1, 1, 2, 2, 3, 2};
1, 2, 3, 0, 0, 0
public class ArrayDuplicates {
public static void main(String args) {
int array = {1, 1, 2, 2, 3, 2};
System.out.println(Arrays.toString(deleteArrayDuplicates(array)));
}
public static int deleteArrayDuplicates(int array) {
for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[i] == array[j]) { //this is for comparing elements
for (; i > 0; i--) {
array[j + 1] = array[j]; //this is for shifting
}
array[array.length - 1] = 0; //making last element equal to "0"
}
}
}
return array;
}
}
But it doesn't work.. Is anyone familiar with a right solution?
I appreciate your assistance and attention very much.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.