Given an array and a value, remove all instances of that value in place and return the new length.

The order of elements can be changed. It doesn’t matter what you leave beyond the new length.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Solution {
public int removeElement(int[] nums, int val) {
int length = nums.length, i = 0, end = length - 1;

while (i <= end){
if (nums[i] == val){
nums[i] = nums[end];
end--;
length--;
}else{
i++;
}
}
return length;
}
}

Most voted solution on Leetcode

1
2
3
4
5
6
7
int removeElement(int A[], int n, int elem) {
int begin = 0;
for(int i = 0; i < n; i++) {
if(A[i] != elem) A[begin++] = A[i];
}
return begin;
}