-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicatesFromSortedArrayII.java
More file actions
206 lines (164 loc) · 5.29 KB
/
RemoveDuplicatesFromSortedArrayII.java
File metadata and controls
206 lines (164 loc) · 5.29 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package Algorithms.IntegerArray;
import java.util.Arrays;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 08 June 2025
* @link 80. Remove Duplicates from Sorted Array II https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
* @topics Array, Two Pointers
* @description Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length - maintain at most two occurrences of each element.
* @see Algorithms.IntegerArray.RemoveDuplicatesFromSortedArray - maintain only one occurrence of each element
*/
public class RemoveDuplicatesFromSortedArrayII {
public static void main(String[] args) {
int[] nums = {1,1,1,2,2,2,3,3};
System.out.println("Array before removeDuplicates() " + Arrays.toString(nums));
int newLen = removeDuplicates(nums);
System.out.println("Array after removeDuplicates() with newLen = " + newLen + " => " + Arrays.toString(nums));
nums = new int[]{1,1,1,2,2,2,3,3};
System.out.println("Array before removeDuplicates() " + Arrays.toString(nums));
newLen = removeDuplicatesMyApproach(nums);
System.out.println("Array after removeDuplicatesMyApproach() with newLen = " + newLen + " => " + Arrays.toString(nums));
}
/**
* count = the number of occurrences of same element
* 2 pointers l and r
*
* even after count while-loop, we need 'r' to be in that sameNum
*/
public static int removeDuplicates(int[] nums) {
int n = nums.length, l=0, r=0, count=1;
if(n<=2) return n;
while(r<n) {
// count the sameNum occurrences
while(r+1<n && nums[r]==nums[r+1]) { // nums[currR] == nums[nextR]
count++;
r++; // increment r, only if(nums[currR] == nums[nextR])
}
for(int i=0; i<Math.min(2,count) && l<n && r<n; i++, l++) { // i<Math.min(2,count), cause we need max of 2 occurrences
nums[l] = nums[r]; // we can skip if(nums[l] == nums[r]) -> optional
}
r++;
count=1;
}
return l;
}
/**
* Given
0 1 2 3 4 5 6 7
[1,1,1,2,2,2,3,3]
Use two pointers, k and i
i=2
[1,1,1,2,2,2,3,3] ---> nums[k - 2] != nums[i] -> 1 != 1
k
i
i=3
[1,1,1,2,2,2,3,3] ---> nums[k - 2] != nums[i] -> 1 != 2 -> "swap" && k++
k
i
i=4
[1,1,2,2,2,2,3,3] ---> nums[k - 2] != nums[i] -> 1 != 2 -> "swap" && k++
k
i
i=5
[1,1,2,2,2,2,3,3] ---> nums[k - 2] != nums[i] -> 2 != 2
k
i
i=6
[1,1,2,2,2,2,3,3] ---> nums[k - 2] != nums[i] -> 2 != 3 -> "swap" && k++
k
i
i=7
[1,1,2,2,3,2,3,3] ---> nums[k - 2] != nums[i] -> 2 != 3 -> "swap" && k++
k
i
i=8 - end of array
[1,1,2,2,3,3,3,3]
k
i
*/
public static int removeDuplicates2(int[] nums) {
int k = 2; // Start placing elements at the third position
if (nums.length <= k) return nums.length;
for (int i=2; i < nums.length; i++) {
// If current element differs from element two positions back, include it
if (nums[i] != nums[k - 2]) {
nums[k] = nums[i];
k++;
}
}
return k;
}
/**
same number can appear once or twice but not more than twice
Given
[1,1,1,1,2,2,3]
START A PARENT WHILE LOOP (r<n) -----
l
need (l-1 < l) || (l-2 == l-1 == l)
while( l<n && nums[l-1]==nums[l] && l>1?(nums[l-2]<=nums[l]):true) l++;
r
need (r-1 != r) || (r-1==r && r-2 != r)
while(r<n && ((r-1 != r) || (r-1==r && r-2 != r))) r++;
swap
n[l]=n[r]
l++
r++
l=2, r=3;
l-loop
[1,1,1,1,2,2,3]
l r
r-loop
[1,1,1,1,2,2,3]
l r
swap && increment
[1,1,2,1,2,2,3]
l r
l-loop
[1,1,2,1,2,2,3]
l r
r-loop
[1,1,2,1,2,2,3]
l r
swap && increment
[1,1,2,2,2,1,3]
l r
l-loop
[1,1,2,2,2,1,3]
l r
r-loop
[1,1,2,2,2,1,3]
l r
swap && increment
[1,1,2,2,3,1,3]
l r
*/
public static int removeDuplicatesMyApproach(int[] nums) {
int n = nums.length, l=2, r=3;
if(n==2) return 2;
while(l<n && r<n) {
while(l<n && !(nums[l-2] == nums[l] || nums[l-1] > nums[l])) l++;
/**
or
while(l<n) {
if(nums[l-2]==nums[l] || nums[l-1]>nums[l]) break;
l++;
}
*/
if(l>r) r=l+1;
while(r<n && (nums[l]==nums[r] || nums[l-2]==nums[r]) ) r++;
if(l<n && r<n) {
int temp = nums[l];
nums[l]=nums[r];
nums[r]=temp;
}
l++;
r++;
}
int k = 2;
for(int i=2; i<n; i++) {
if(nums[i-1]>nums[i] || nums[i-2]==nums[i-1] && nums[i-1]==nums[i]) break;
k++;
}
return k;
}
}