27. Remove Element
Last updated
Last updated
public int removeElement(int[] nums, int val) {
// This will track the occurrence count of the val
int placesToBeMovedBy = 0;
for (int i = 0; i < nums.length; ++i) {
// If current value is val increase the occurrence count and continue
if(nums[i] == val) {
placesToBeMovedBy++;
continue;
}
// If there is an occurence of val we can overwrite it
nums[i - placesToBeMovedBy] = nums[i];
}
return nums.length - placesToBeMovedBy;
}