-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_element.js
More file actions
37 lines (31 loc) · 790 Bytes
/
Copy pathremove_element.js
File metadata and controls
37 lines (31 loc) · 790 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
var removeElement = function(nums, val) {
let index = 0;
let length = nums.length
nums.sort(function(a, b){return a-b});
const p = nums.indexOf(val);
if(p === -1){return length}
for(let i = p; i<nums.length; i++){
if(nums[i]=== val){
nums[i]=Infinity
index++
}else{break}
}
nums.sort(function(a, b){return a-b});
return length - index
};
nums = [0,1,2,2,3,0,4,2]
var removeElement = function(nums, val) {
nums.sort(function(a, b){return a-b});
let i = nums.indexOf(val);
let n = nums.length;
while (i < n) {
if (nums[i] === val) {
nums[i] = nums[n - 1];
n--;
} else {
i++;
}
}
return n;
};
console.log(removeElement(nums,2))