-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRemoveDuplicate.java
More file actions
37 lines (34 loc) · 1014 Bytes
/
RemoveDuplicate.java
File metadata and controls
37 lines (34 loc) · 1014 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
37
public class RemoveDuplicate {
public int removeDuplicates(int[] A) {
// Start typing your Java solution below
// DO NOT write main() function
int len = A.length;
int j;
for(int i = 0; i < len; i++){
j = i + 1;
while(j < len && A[j] == A[i]) j++;
if(j - i > 1){ // then we should move the same element
int move = j - i - 1;
for(int k = j; k < len; k++)
A[k - move] = A[k];
len -= move ;
}
}
return len;
}
public int removeElement(int[] A, int elem) {
// Start typing your Java solution below
// DO NOT write main() function
int len = 0;
for(int i = 0; i < A.length; i++){
if(A[i] != elem)
A[len++] = A[i];
}
return len;
}
public static void main(String[] args){
RemoveDuplicate rd = new RemoveDuplicate();
int[] A = {1,1,2};
System.out.println(rd.removeDuplicates(A));
}
}