-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremove-element.js
145 lines (130 loc) · 3.32 KB
/
remove-element.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* Source: https://leetcode.com/problems/remove-element/
* Tags: [Array,Two Pointers]
* Level: Easy
* Title: Remove Element
* Auther: @imcoddy
* Content: 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.
*/
/**
* @param {number[]} A
* @param {number} elem
* @returns {number}
*/
/**
* Explanation: Using javascript API to remove element directly
* Runtime: 161ms
* Rank: B
*/
/*
*var removeElement = function(A, elem) {
* if (!A || A.length <= 0) {
* return 0;
* }
*
* while (A.indexOf(elem) !== -1) {
* var index = A.indexOf(elem);
* A.splice(index, 1);
* }
* console.log(A);
* return A.length;
*};
*/
/**
* Runtime: 171ms
* Rank: C
*/
var removeElement = function(A, elem) {
if (!A || A.length <= 0) {
return 0;
}
var tail = A.length - 1;
var head = 0;
while (head < tail) {
while (A[head] !== elem && head < tail) {
head++;
}
while (A[tail] === elem) {
tail--;
}
if (head < tail) {
var temp = A[head];
A[head] = A[tail];
A[tail] = temp;
}
}
if (A[tail] === elem) {
tail--;
}
return tail + 1;
};
/**
* Memo: Use Javascript API to locate the element in array and delete it.
* Complex: O(n)
* Runtime: 128ms
* Tests: 112 test cases passed
* Rank: S
* Updated: 2015-06-15
*/
var removeElement = function(nums, val) {
var index;
while ((index = nums.indexOf(val)) !== -1) {
nums.splice(index, 1);
}
return nums.length;
};
/**
* Shorter version of the above
*/
var removeElement = function(nums, val) {
while ((nums.indexOf(val)) !== -1) nums.splice(nums.indexOf(val), 1);
return nums.length;
};
/**
* Memo: Keep moving head and tail pointers until they match
* Complex: O(n)
* Runtime: 140ms
* Tests: 112 test cases passed
* Rank: B
* Updated: 2015-06-15
*/
var removeElement = function(nums, val) {
var head = 0;
var tail = nums.length;
while (head < tail) {
if (nums[head] === val) {
nums[head] = nums[--tail];
} else {
head++;
}
}
return tail;
};
/**
* Shorter version of the above
*/
var removeElement = function(nums, val) {
var head = 0,
tail = nums.length;
while (head < tail) nums[head] = nums[head] === val ? nums[--tail] : nums[head++];
return tail;
};
var should = require('should');
console.time('Runtime');
removeElement([2], 2).should.equal(0); // 0
removeElement([2], 3).should.equal(1); // 1
removeElement([3, 3], 3).should.equal(0); // 0
removeElement([3, 3], 5).should.equal(2); // 2
removeElement([1, 3, 1, 1, 2, 1, 1, 1], 1).should.equal(2); // 2
removeElement([1, 1, 1, 1, 1, 1, 1, 1], 1).should.equal(0); // 0
removeElement([2, 1, 1, 1, 1, 1, 1, 1], 1).should.equal(1); // 1
removeElement([1, 1, 1, 1, 2, 2, 2, 2], 1).should.equal(4); // 4
removeElement([2, 1, 1, 1, 2, 2, 2, 2], 1).should.equal(5); // 5
removeElement([1, 1, 1, 1, 1, 1, 1, 2], 1).should.equal(1); // 1
removeElement([1, 1, 1, 1, 1, 1, 1, 2], 2).should.equal(7); // 7
removeElement([2, 2, 2, 2, 2, 2, 2, 2], 1).should.equal(8); // 8
console.timeEnd('Runtime');