Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory.
I came up with below solution but this doesn't return correct output - I mean my input array still has duplicate values in it? Is there anything wrong I did?
public class Solution {
public static void main(String[] args) {
int[] input = new int[] { 1, 1, 3, 7, 7, 8, 9, 9, 9, 10 };
int length = input.length;
if (length == 0 || length == 1) {
System.out.println(length);
System.exit(0);
}
int i = 1;
for (int j = 1; j < length; j++) {
if (input[j] != input[j - 1]) {
input[i] = input[j];
i++;
}
}
if (i < length) {
input[i] = '\0';
}
System.out.println(i);
System.out.println(Arrays.toString(input));
}
}
NOTE: This is not homework, just preparing for an interview.