-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove-duplicates-from-sorted-array-ii.js
More file actions
125 lines (97 loc) · 2.67 KB
/
remove-duplicates-from-sorted-array-ii.js
File metadata and controls
125 lines (97 loc) · 2.67 KB
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
/**
* Problem: Remove Duplicates from Sorted Array II
* Link: https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
* Difficulty: Medium
*
* Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place
* such that each unique element appears at most twice. Return the number of elements after removal.
*
* Example:
* Input: nums = [1,1,1,2,2,3]
* Output: 5, nums = [1,1,2,2,3,_]
*
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
// JavaScript Solution - Two Pointers with Count
function removeDuplicates(nums) {
if (nums.length <= 2) return nums.length;
let k = 2; // Start from index 2 since first two are always valid
for (let i = 2; i < nums.length; i++) {
// Compare with element 2 positions back
if (nums[i] !== nums[k - 2]) {
nums[k] = nums[i];
k++;
}
}
return k;
}
// Alternative approach with explicit counting
function removeDuplicatesWithCount(nums) {
if (nums.length === 0) return 0;
let k = 1;
let count = 1;
for (let i = 1; i < nums.length; i++) {
if (nums[i] === nums[i - 1]) {
count++;
} else {
count = 1;
}
if (count <= 2) {
nums[k] = nums[i];
k++;
}
}
return k;
}
// Test cases
let nums = [1, 1, 1, 2, 2, 3];
console.log(removeDuplicates(nums)); // 5
console.log(nums.slice(0, 5)); // [1,1,2,2,3]
nums = [0, 0, 1, 1, 1, 1, 2, 3, 3];
console.log(removeDuplicates(nums)); // 7
console.log(nums.slice(0, 7)); // [0,0,1,1,2,3,3]
module.exports = removeDuplicates;
/* Python Solution (commented):
def remove_duplicates(nums: list[int]) -> int:
"""
Remove duplicates allowing at most 2 occurrences
Args:
nums: Sorted array of integers
Returns:
Number of elements after removal
Time Complexity: O(n)
Space Complexity: O(1)
"""
if len(nums) <= 2:
return len(nums)
k = 2 # Start from index 2
for i in range(2, len(nums)):
# Compare with element 2 positions back
if nums[i] != nums[k - 2]:
nums[k] = nums[i]
k += 1
return k
# Alternative with explicit counting
def remove_duplicates_with_count(nums: list[int]) -> int:
if not nums:
return 0
k = 1
count = 1
for i in range(1, len(nums)):
if nums[i] == nums[i - 1]:
count += 1
else:
count = 1
if count <= 2:
nums[k] = nums[i]
k += 1
return k
# Test cases
nums = [1,1,1,2,2,3]
k = remove_duplicates(nums)
print(k, nums[:k]) # 5 [1, 1, 2, 2, 3]
nums = [0,0,1,1,1,1,2,3,3]
k = remove_duplicates(nums)
print(k, nums[:k]) # 7 [0, 0, 1, 1, 2, 3, 3]
*/