27 - Remove Element
Written on October 21, 2015
Tweet
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.
public class Solution {
public int removeElement(int[] nums, int val) {
int i = 0;
for(int n : nums){
if(n != val){
//if n != val, we increment i
nums[i++] = n;
}
//if n==val, we don't increment i, and nums[i] will be overwritten by n
}
return i;
}
}
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
if not nums:
return 0
index = 0
for num in nums:
if num != val:
nums[index] = num
index += 1
return index