-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25-removeDuplicatesFromSortedArray.js
More file actions
47 lines (38 loc) · 1016 Bytes
/
25-removeDuplicatesFromSortedArray.js
File metadata and controls
47 lines (38 loc) · 1016 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
44
45
46
47
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function (nums) {
if (nums.length === 0) {
return [];
}
let banishToTheEndOfTheArray = (index) => {
let temp = nums[index];
let bubbleIndex = index + 1;
while (bubbleIndex < nums.length) {
nums[bubbleIndex - 1] = nums[bubbleIndex];
bubbleIndex++;
}
nums[nums.length - 1] = temp;
};
let noBanished = 0;
for (let i = 0; i < nums.length; i++) {
if (i === nums.length - 1) {
return nums.length;
}
if (nums[i + 1] < nums[i]) {
return i + 1;
}
if (nums[i + 1] === nums[i]) {
banishToTheEndOfTheArray(i + 1);
noBanished++;
if (i + 1 + noBanished === nums.length) {
return i + 1;
}
i--;
}
}
};
let arr = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4];
console.log(removeDuplicates(arr));
console.log(arr);