DEV Community

Tomer Ben David
Tomer Ben David

Posted on • Updated on

Build a PowerSet

What is a PowerSet?

Why is it called a PowerSet?
Is it good or bad for us?
Will I need it on my next Angular/Machine learning project?

If we look at Wikipedia for the definition of powerset we see the below:

In mathematics, the power set (or powerset) of any set S is the set of all subsets of S,...

Cool, so that means if we have a set of a few item's {1, 2} then the power set would be the set of all sets, in the {1, 2} case it would be:

{ {1}, {2}, {1, 2}}

Continuing to read the Wikipedia definition we see the guys there say:

including the empty set and S itself

You know what, those guys say we need to add the empty set let's just add it and move on:

{ {}, {1}, {2}, {1, 2}}

So we added the empty set, the definition is now happy. :)

All right I think now we are aligned with the definition, nice.

Computer makes a power set

Now the question is, how do we program the computer, to give us the PowerSet? The computer did not read the Wikipedia definition, so it doesn't have a clue about how to code that, let's help him.

Recursion comes immediately to mind. With recursion, if we manage the solve the problem in terms of a smaller problem, we could merge the smaller problem solution with the larger one, so we take our original set:

{1, 2}, a smaller set or a smaller problem here would be: {1, 2}.tail => {2}. If we magically (or in other words - recursively) new the PowerSet of it:

{ {}, {2}} . All that would be left to us then would be to merge it with the PowerSet of the head - {1, 2}.head => {1} which is: { {}, {1}} and then add the set itself which is: { {1, 2} }.

I don't know about you but, as much as recursion is romantic, I always feel like it's cheating, so how would we do it without cheating, that is, without recursion?

If we scan the set only once and do a single calculation on each item, we wouldn't really have a chance to do all the combinations of the subsets, so it appears as if we need to have nested loop. Let's start writing that, but wait before that, let's warm up our java syntax engines a bit.

Power Set with Java

Warming up java set skills:

Define a new set in java:

Set<Integer> set = new HashSet<>(); // define a set of integers in java.
Set<Set<Integer>> setOfSets = new HashSet<>(); // PowerSet is a set of sets..
Set<Set<Integer>> copiedSet = new HashSet(setOfSets); // Copy a set into a new variable.

We begin with the signature, we get a set and we return a set, both are a set of integers:

public Set<Set<Integer>> powerSet(Set<Integer> set) // we get a set and return a set of sets, a power set is a set of sets.

Now that we got a set let's start iterating the set items:

public Set<Set<Integer>> powerSet(Set<Integer> set) {
    Set<Set<Integer> powerSet = new HashSet<>();

    for (Integer item: set) {

    // omg omgI'm stuck here...
    // but now what after we added all items how do we combine them all to the power set?

It looks like this is not enough, we cannot just scan the items and build a set of sets. Let's ponder about this a little.


Well we said a set of sets, let's repeat this a set of sets, so it's like multiple sets but not only multiple sets it's like each set is including other sets.

Each set that we manage to create, we are going to need this set with each other item in the set, so see, for each set we find, no matter which subset it is, for each subset, another valid subset would be this subset with each other item on the original set.

Found { 1, 2 } subset? and you have { 4 } in the original set: here is another subset for you { 1, 2, 4 }.

For that reason what we are going to do is store all the temporal subsets we manage to find and for each such subset add each item from the original set meaning it's going to follow:

  1. Find the first subset - for example, the empty set.
  2. Add it to the subsets already found.
  3. Add each item from original set to.

Alright, all set? let's move on to the full PowerSet Implementation!:

     public static Set<Set<Integer>> powerSet(Set<Integer> set) {
       Set<Set<Integer>> powerSets = new HashSet<>();  // we define powerset.

       Set<Integer> emptySet = new HashSet<>();
       powerSets.add(emptySet); // start with the empty set.

       for (Integer item: set) { // We want to add each item to all previous subsets.
         Set<Set<Integer>> foundSets = new HashSet(powerSets);
         for (Set<Integer> foundSet: foundSets) {
           Set<Integer> newSet = new HashSet<Integer>(foundSet);
           newSet.add(item); // Add each item to all previously found subsets.
           powerSets.add(newSet); // Add new subset to all subsets found.


       return powerSets;


And if we run it with:

         Set<Integer> set = new HashSet<Integer>();

We get:

// [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]] Looks like a power set to me! What do you say? ;)

Back to recursion

Now that we have the imperative method behind us, let's see the scala :: functional :: declarative :: concise method :))

  def powerSet(set: Set[Int]): Set[Set[Int]] = {

    set.toList match {
      case Nil => Set(Set.empty[Int])
      case x :: xs => powerSet(xs.toSet).map(xsi => xsi + x) ++ powerSet(xs.toSet)

  println(powerSet(Set(1,2,3))) // Set(Set(), Set(3, 1), Set(2), Set(2, 1), Set(3, 2), Set(3), Set(3, 2, 1), Set(1))

So cool! we just spelled the magic words of the powerSet definition, that each powerset is the powerset of the head added to each of the powerset found for the tail! and we have it done and ready! so hot!


To sum up we started by adding the empty set. Then we scan each item from 1..n and add this item to the empty set, we end up having each time only once in the powerSet.

Next, we take each such item and add it to the currently existing sets, when we add { 1 } to the set { 1 } it would not be { 1, 1 } with just { 1 } (psst it's a set remember?). And we continue and scan each item in the set and add each of them to the current sets which would mean we get to have all subsets - power sets.

How will that help us with AngularJs or machine learning? it's simple at least in my mind, by mastering syntax and the language, by mastering the basic data structures, be it sets, lists, maps, from them we can build queues, heaps, trees, and from them even more complex data structures all these are important, and as software engineers we deal with them every day, either directly or indirectly, so it's best to master those very simple basic terms.

The for loop, the set, the list, are basic, but they are powerful with these basic constructs we have built the powerSet.

Top comments (6)

carstenk_dev profile image
Carsten • Edited

Maybe it's enlightening to see a functional take on that as well.
If you think a bit about this, producing all members of a powerset basically comes down to go over all elements of the input-set.

For every such you have the choice of including it in a subset or not (so you can directly see that there will be 2n subsets for a set of size n)

This idea directly translates into this (yeah sorry it's Haskell):

powerset []     = [ [] ]
powerset (x:xs) = map (x:) (powerset xs) ++ powerset xs


  • if you've got the empty set (really a list here but hey FP right?) the only subset of this is the empty-subset itself

  • or else you have a set with at least one element x (and possible more elements xs)

    • now you have two classes ob subsets: one where x is included, and one where it is not
    • you get both by recursively getting all the subsets of the remaining elements xs - just include x everywhere in one copy (that's the map (x:) part - ++ just concatenates the two sets ob subsets

call me weird but I consider that easier to read and reason about than the imperative Java solution ;)

tomerbendavid profile image
Tomer Ben David • Edited

This is definitely beautiful! :)

This is what I like about declarative and functional code :)

It's pure :)

I have to add that I have a place also for imperative code, what I do like about imperative code is that instead of repeating the definition (which is not a bad thing it's beautiful), I'm actually taking the mini steps and it makes me understand what's happening better. (that's at least how I understand things).

In addition, let's say I to log something out to the logger, I'm not sure what would happen to the functional code, or I would need to send some metrics to monitoring, I'm sure there is a functional solution to it (monad and friends) but this is where things get's heavy on me.

carstenk_dev profile image

depends on what you want to log I guess (seems doubtful, that you want to log anything here - indeed I never had the need to log anything inside a pure function as you can test it anytime if you know the input to it)

but sure most of us learned programming in the more operational/imperative mindset (basically by doing step-by-step debugging in our head) so it might take some time to get "warm" with FP ;)

But in those more mathematical problems (where the problem often is recursive in nature) it's just a natural fit ;)

Thread Thread
tomerbendavid profile image
Tomer Ben David • Edited

cool, adding the scala functional :: recursive :: declarative :: concise way to the post :)) .

  def powerSet(set: Set[Int]): Set[Set[Int]] = {

    set.toList match {
      case Nil => Set(Set.empty[Int])
      case x :: xs => powerSet(xs.toSet).map(xsi => xsi + x) ++ powerSet(xs.toSet)

  println(powerSet(Set(1,2,3))) // Set(Set(), Set(3, 1), Set(2), Set(2, 1), Set(3, 2), Set(3), Set(3, 2, 1), Set(1))
tobias_salzmann profile image
Tobias Salzmann

I remember this firmly as one of the more difficult exercises in my introductory programming course at university. But finding that solution was so rewarding!

idanarye profile image
Idan Arye

A subset of a N-sized set can be represented with an N-bit binary number, where each bit determines whether or not the subset contains its corresponding member. By incrementing the binary representation we can easily iterate over all the possible subsets.

Here is a simple implementation:

    public static <T> Set<Set<T>> powerSet(Set<T> set) {
        // We need to have them ordered, to match the bits of the binary rep.
        ArrayList<T> values = new ArrayList<>(set);
        boolean[] binaryRep = new boolean[values.size()];

        Set<Set<T>> powerSet = new HashSet<>();

        while (true) {
            Set<T> subset = createSubset(binaryRep, values);
            if (increment(binaryRep)) {
                // Iterated over all the numbers.

        return powerSet;

    private static <T> Set<T> createSubset(boolean[] binaryRep, List<T> values) {
        Set<T> subset = new HashSet<>();
        for (int i = 0; i < binaryRep.length; ++i) {
            if (binaryRep[i]) {
        return subset;

    private static boolean increment(boolean[] binaryRep) {
        for (int i = 0; i < binaryRep.length; ++i) {
            if (binaryRep[i]) {
                // Make the 1 bit a 0 and carry the 1.
                binaryRep[i] = false;
            } else {
                // Finally reached a 0 bit - set it to 1 and we are done.
                binaryRep[i] = true;
                return false;
        // All the bits were 1 and we turned them to 0 - that means we "overflowed".
        return true;