-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicates.java
More file actions
55 lines (42 loc) · 1.04 KB
/
Copy pathRemoveDuplicates.java
File metadata and controls
55 lines (42 loc) · 1.04 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
package removeDuplicates;
public class RemoveDuplicates {
public static void main(String[] args) {
int[] nums = {1,2,3,4,5,6};
int len = removeDuplicates(nums);
for (int i = 0;i < len; i++) {
System.out.println("element: " + nums[i]);
}
}
public static int removeDuplicates(int[] nums) {
if (nums.length == 0)
return 0;
if (nums.length == 1)
return 1;
int maxNum = nums[nums.length - 1];
sortDuplicate(nums);
for (int i = 0; i < nums.length; i++) {
if (maxNum == nums[i]) {
return i+1;
}
}
return 0;
}
public static void sortDuplicate(int[] nums) {
int i = 0;
int j = i +1;
while (j < nums.length) {
if (nums[i] == nums[j]) {
nums[j] = Integer.MIN_VALUE;
} else if (nums[i] < nums[j]) {
swap(nums, i+1, j);
i++;
}
j++;
}
}
public static void swap(int[] nums, int start, int end) {
int tmp = nums[start];
nums[start] = nums[end];
nums[end] = tmp;
}
}