-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistinctSubsets.java
More file actions
29 lines (25 loc) · 917 Bytes
/
DistinctSubsets.java
File metadata and controls
29 lines (25 loc) · 917 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
import java.util.*;
public class DistinctSubsets {
public static List<List<Integer>> findSubsets(int[] arr) {
Arrays.sort(arr);
Set<List<Integer>> subsets = new HashSet<>();
int n = arr.length;
int totalSubsets = (1 << n);
for (int mask = 0; mask < totalSubsets; mask++) {
List<Integer> subset = new ArrayList<>();
for (int i = 0; i < n; i++) {
if ((mask & (1 << i)) != 0) {
subset.add(arr[i]);
}
}
subsets.add(subset);
}
return new ArrayList<>(subsets);
}
public static void main(String[] args) {
int[] arr1 = {1, 2, 2};
int[] arr2 = {1, 2};
System.out.println("Subsets for {1, 2, 2}: " + findSubsets(arr1));
System.out.println("Subsets for {1, 2}: " + findSubsets(arr2));
}
}