I am trying to remove target element from an array , if input is [3,3,2,2,3,3] and my target is 3 , I should get [2,2]. the logic is simple, using slow and fast pointers and if we need copy, move slow pointer, otherwise move fast pointer to skip the target .
but I am keep getting index out of bound error and can not figure out why ?
public int[] removeElement(int[] input, int value) {
int slow = 0;
int fast = 0;
while (fast < input.length) {
while (input[fast] == value && fast < input.length) {
fast++;
}
input[slow++] = input[fast++];
}
return Arrays.copyOfRange(input, 0, slow);
}