-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicates.java
More file actions
195 lines (174 loc) · 3.5 KB
/
RemoveDuplicates.java
File metadata and controls
195 lines (174 loc) · 3.5 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
package codingInterview;
import java.util.Arrays;
public class RemoveDuplicates {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = {1, 1, 1, 2, 2, 3};
// int count = removeDuplicates1(a);
// System.out.println(count);
// int[] newArray = removeDuplicates2(a);
// printArray(newArray);
// int count = removeDuplicates3(a);
// System.out.println(count);
// int count = removeDuplicates4(a);
// printArray(a);
// System.out.println(count);
// int count = removeDuplicates5(a);
// printArray(a);
// System.out.println(count);
int count = removeDuplicates6(a);
printArray(a);
System.out.println(count);
}
/**
* modify the array in place, and return the allowed number of array
* @param a
* @return
*/
private static int removeDuplicates6(int[] a) {
// TODO Auto-generated method stub
if (a == null || a.length == 0) {
return 0;
}
int prev = 1;
int cur = 2;
while (cur < a.length) {
if (a[cur] == a[prev] && a[cur] == a[prev - 1]) {
cur++;
} else {
prev++;
a[prev] = a[cur];
cur++;
}
}
return prev + 1;
}
/**
* modify the array in place, and return the allowed number fo array
* @param a
* @return
*/
private static int removeDuplicates5(int[] a) {
// TODO Auto-generated method stub
if (a == null || a.length == 0) {
return 0;
}
int pre = a[0];
boolean flag = false;
int count = 0;
int j = 1;
for (int i = 1; i < a.length; i++) {
int cur = a[i];
if (cur == pre) {
if (!flag) {
flag = true;
a[j++] = cur;
continue;
} else {
count++;
}
} else {
flag = false;
a[j++] = cur;
}
pre = cur;
}
return a.length - count;
}
/**
* duplicates are allowed at most twice
* naive, only return the allowed number of the array, don't change the original array
* @param a
* @return
*/
private static int removeDuplicates4(int[] a) {
// TODO Auto-generated method stub
if (a == null || a.length == 0) {
return 0;
}
int pre = a[0];
boolean flag = false;
int count = 0;
for (int i = 1; i < a.length; i++) {
int cur = a[i];
if (pre == cur) {
if (!flag) {
flag = true;
continue;
} else {
count++;
}
} else {
flag = false;
}
pre = cur;
}
return a.length - count;
}
/**
* only return the allowed number of the array
* @param a
* @return
*/
private static int removeDuplicates3(int[] a) {
// TODO Auto-generated method stub
int i = 0;
int j = 0;
while (i < a.length - 1) {
if (a[i] == a[i + 1]) {
j++;
}
i++;
}
return a.length - j;
}
/**
* return the new array containing the unique elements
* @param a
* @return
*/
private static int[] removeDuplicates2(int[] a) {
// TODO Auto-generated method stub
int i = 1;
int j = 0;
while (i < a.length) {
if (a[i] == a[j]) {
i++;
} else {
j++;
a[j] = a[i];
i++;
}
}
int[] newArrasy = Arrays.copyOf(a, j + 1);
return newArrasy;
}
/**
* only return the number of the unique elements
* @param a
* @return
*/
private static int removeDuplicates1(int[] a) {
// TODO Auto-generated method stub
int i = 1;
int j = 0;
while (i < a.length) {
if (a[i] == a[j]) {
i++;
} else {
j++;
a[j] = a[i];
i++;
}
}
printArray(a);
return j + 1;
}
private static void printArray(int[] a) {
// TODO Auto-generated method stub
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
}