-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprblm26.java
More file actions
38 lines (34 loc) · 1008 Bytes
/
prblm26.java
File metadata and controls
38 lines (34 loc) · 1008 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
38
import java.util.*;
public class prblm26 {
public static void main(String[] args) {
int[] nums = {-3,-1,0,0,0,3,3};
int ans = removeDuplicates(nums);
System.out.println(ans);
}
public static int removeDuplicates(int[] nums) {
Set<Integer> set = new HashSet<>();
for(int i = 0; i < nums.length; i++){
set.add(nums[i]);
}
List<Integer> sortedSet = new ArrayList<>(set);
Collections.sort(sortedSet);
int idx = 0;
for(int val : sortedSet){
nums[idx] = val;
idx++;
}
System.out.println(Arrays.toString(nums));
return set.size();
}
public static int removeDuplicates2(int[] nums) {
int idx = 0;
for(int val : nums){
if(idx == 0 || val != nums[idx - 1]){
nums[idx] = val;
idx++;
}
}
System.out.println(Arrays.toString(nums));
return idx;
}
}