Traverse nums
, if we encounter an element that equals val
we skip it but keep a flag on its index for later.
Using a flag variable n
to keep track of the last index in which the numbers from 0
up to n-1
are not equal to val
:
- Whenever we encounter an element that is equal to
val
, we skip it and move forward. - else (element not equal to
val
), then we store it atn
and moven
forward. - By the end all non
val
elements will be atnums[0:n-1]
-
Time complexity: O(k): where k is the size of
nums
. -
Space complexity: O(1): No extra space is needed.
class Solution {
public int removeElement(int[] nums, int val) {
int n = 0;
for(int i = 0 ; i < nums.length; i++)
if(nums[i]!=val)
nums[n++] = nums[i];
return n;
}
}