Skip to content

Commit 94d3c27

Browse files
committed
Remove Duplicated From Sorted Array solution
1 parent dbf2734 commit 94d3c27

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

25-removeDuplicatesFromSortedArray.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var removeDuplicates = function (nums) {
6+
if (nums.length === 0) {
7+
return [];
8+
}
9+
10+
let banishToTheEndOfTheArray = (index) => {
11+
let temp = nums[index];
12+
let bubbleIndex = index + 1;
13+
14+
while (bubbleIndex < nums.length) {
15+
nums[bubbleIndex - 1] = nums[bubbleIndex];
16+
bubbleIndex++;
17+
}
18+
19+
nums[nums.length - 1] = temp;
20+
};
21+
22+
let noBanished = 0;
23+
for (let i = 0; i < nums.length; i++) {
24+
if (i === nums.length - 1) {
25+
return nums.length;
26+
}
27+
28+
if (nums[i + 1] < nums[i]) {
29+
return i + 1;
30+
}
31+
32+
if (nums[i + 1] === nums[i]) {
33+
banishToTheEndOfTheArray(i + 1);
34+
noBanished++;
35+
if (i + 1 + noBanished === nums.length) {
36+
return i + 1;
37+
}
38+
i--;
39+
}
40+
}
41+
};
42+
43+
let arr = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4];
44+
45+
console.log(removeDuplicates(arr));
46+
47+
console.log(arr);

0 commit comments

Comments
 (0)