-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path27. Remove Element.js
More file actions
43 lines (35 loc) · 814 Bytes
/
Copy path27. Remove Element.js
File metadata and controls
43 lines (35 loc) · 814 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* @param {number[]} nums
* @param {number} val
* @return {number}
*/
// [3,1,2,3,4,5,6]
var removeElement = function(nums, val) {
if(nums.length === 0) return 0;
//if(nums.indexOf(val) < 0) return nums.length;
let newlength = 0;
let nl = nums.length;
for(let i=0; i<nl ; i++){
if(nums[i] !== val) {
nums[newlength] = nums[i];
newlength++;
}
}
return newlength;
};
/**
* @param {number[]} nums
* @param {number} val
* @return {number}
*/
var removeElement = function(nums, val) {
if (nums instanceof Array === false) throw new Error("input error");
var pt = 0;
for (var i=0; i<nums.length; i++) {
if (nums[i] !== val) {
nums[pt] = nums[i];
pt += 1;
}
}
return pt;
};