-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicate.java
More file actions
34 lines (32 loc) · 961 Bytes
/
RemoveDuplicate.java
File metadata and controls
34 lines (32 loc) · 961 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
public class RemoveDuplicate {
/**
* Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
* @param A
* @return
*/
public int removeDuplicates(int[] A) {
// array is sorted so we know the range of array is [x...n]
int len = A.length;
if (len > 1) {
int first = A[0];
int last = A[len - 1];
len = Math.abs(last - first) + 1;
int start = 0;
int end = 1;
while (start != len - 1) {
while (A[start] == A[end]) {
end++; //find the range of duplicate <Start ... end>
}
for (int i = start + 1, j = end; i < end; i++) {
A[i] = A[j]; //Replace the content
}
start = start + 1; //increment to next position
}
}
return len;
}
}