-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubsets.java
More file actions
30 lines (28 loc) · 912 Bytes
/
subsets.java
File metadata and controls
30 lines (28 loc) · 912 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class subsets {
public static void main(String[] args) {
int[] arr = {1,2,2};
System.out.println(susbsetDuplicate(arr));
}
private static List<List<Integer>> susbsetDuplicate(int[] arr) {
Arrays.sort(arr);
List<List<Integer>> outer = new ArrayList<>();
outer.add(new ArrayList<>());
int start = 0,end = 0;
for (int i = 0; i < arr.length; i++) {
start = 0;
if(i>0 && arr[i] == arr[i-1])
start = end + 1;
end = outer.size() -1;
int n = outer.size();
for (int j = start; j < n; j++) {
List<Integer> internal = new ArrayList<>(outer.get(j));
internal.add(arr[i]);
outer.add(internal);
}
}
return outer;
}
}