-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveDuplicatesII.js
More file actions
36 lines (30 loc) · 1017 Bytes
/
removeDuplicatesII.js
File metadata and controls
36 lines (30 loc) · 1017 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
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function (nums) {
for (let i = 0; i < nums.length; i++) {
let counter = 1
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] === nums[j]) {
counter++
if (counter > 2) {
nums.splice(i, 1)
j--
}
}
}
}
};
/*
Example 1:
Input: nums = [1,1,1,2,2,3]
Output: 5, nums = [1,1,2,2,3,_]
Explanation: Your function should return k = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
Example 2:
Input: nums = [0,0,1,1,1,1,2,3,3]
Output: 7, nums = [0,0,1,1,2,3,3,_,_]
Explanation: Your function should return k = 7, with the first seven elements of nums being 0, 0, 1, 1, 2, 3 and 3 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
*/