Arrays
Given an integer array
numsand an integerval, remove all occurrences ofvalinnumsin-place. The order of the elements may be changed. Then return the number of elements innumswhich are not equal toval.Consider the number of elements in
numswhich are not equal tovalbek, to get accepted, you need to do the following things:
- Change the array
numssuch that the firstkelements ofnumscontain the elements which are not equal toval. The remaining elements ofnumsare not important as well as the size ofnums.- Return k.
Constraints:
0 <= nums.length <= 1000 <= nums[i] <= 500 <= val <= 100
Example 1
Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,_,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
It does not matter what you leave beyond the returned k (hence they are underscores).
Example 2
Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
Note that the five elements can be returned in any order.
It does not matter what you leave beyond the returned k (hence they are underscores)
Two-pointers are a good approach here. Since swapping values is not required, values other than val should be
put in the nums one by one. One pointer goes one by one, while another pointer increments only when the value
is not val.
class RemoveElement {
public:
int removeElement(vector<int>& nums, int val) {
int idx = 0;
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] != val) {
nums[idx] = nums[i];
idx++;
}
}
return idx;
}
};
class RemoveElement {
public int removeElement(int[] nums, int val) {
int idx = 0;
for (int i = 0; i < nums.length; ++i) {
if (nums[i] != val) {
nums[idx] = nums[i];
idx++;
}
}
return idx;
}
}
/**
* @param {number[]} nums
* @param {number} val
* @return {number}
*/
var removeElement = function(nums, val) {
let idx = 0;
for (let i = 0; i < nums.length; ++i) {
if (nums[i] !== val) {
nums[idx] = nums[i]
idx++;
}
}
return idx;
};
# @param {Integer[]} nums
# @param {Integer} val
# @return {Integer}
def remove_element(nums, val)
idx = 0
0.upto(nums.size - 1) do |i|
if (nums[i] != val)
nums[idx] = nums[i]
idx += 1
end
end
idx
end
O(n)O(1)