https://leetcode.com/problems/remove-element/
Solution 1
/**
* @param {number[]} nums
* @param {number} val
* @return {number}
*/
var removeElement = function (nums, val) {
let remainElement = 0;
for (let i = 0; i < nums.length; i++) {
if (nums[i] === val) {
nums[i] = '_';
} else {
remainElement++;
}
}
nums.sort();
return remainElement;
};
Runtime: 76 ms
Memory Usage: 41.8 MB
Solution 2
/**
* @param {number[]} nums
* @param {number} val
* @return {number}
*/
var removeElement = function (nums, val) {
let samecount = 0;
for (let i = 0; i < nums.length; i++) {
if (nums[i] === val) {
samecount++;
} else {
nums[i - samecount] = nums[i];
}
}
return nums.length - samecount;
};
Runtime: 105 ms
Memory Usage: 42.5 MB