Skip to content

Example: Remove all instances of a value in place and return the new len

Roberto Fronteddu edited this page Mar 17, 2024 · 1 revision

Note how the 2P move at different speed.

public int removeEl(int[] arr, int val) {
    int k = 0;
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] != val) {
            arr[k] = arr[i];
            k++;
        }
    }
    return k;
}
Clone this wiki locally