Skip to content

Commit 28b46d0

Browse files
committed
Find all Subsets of set using Recursion
1 parent 2d63d6a commit 28b46d0

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

SubSetsofSet.java

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
5+
6+
public class SubSetsofSet
7+
{
8+
9+
10+
11+
12+
13+
14+
private List<List<Integer>> subsetsAgain(List<Integer> list, int index){
15+
List<List<Integer>> allSubsets = null;
16+
17+
if(list.size() == index){
18+
allSubsets = new ArrayList<>();
19+
allSubsets.add(new ArrayList<Integer>());
20+
return allSubsets;
21+
}
22+
23+
int value = list.get(index);
24+
allSubsets = subsetsAgain(list, index+1);
25+
26+
List<List<Integer>> output = new ArrayList<>();
27+
for (List<Integer> list2 : allSubsets)
28+
{
29+
List<Integer> newsubset = new ArrayList<>();
30+
newsubset.addAll(list2);
31+
newsubset.add(value);
32+
output.add(newsubset);
33+
}
34+
allSubsets.addAll(output);
35+
36+
return allSubsets;
37+
38+
}
39+
40+
41+
public static void main(String[] args)
42+
{
43+
List<List<Integer>> finaloutput = new SubSetsofSet().subsetsAgain(Arrays.asList(1, 2, 3,4), 0);
44+
for (List<Integer> subsets : finaloutput) {
45+
System.out.println(subsets);
46+
}
47+
}
48+
49+
}

0 commit comments

Comments
 (0)