How can i generate all subsets of a variable length set?
<p>I am trying to write a program that generates all the subsets of an entered set in java. I think i nearly have it working. </p>

<ul>
<li>I have to use arrays (not data structures)</li>
<li>The entered array will never be greater than 20</li>
</ul>

<p>Right now when i run my code this is what i get:</p>

<pre><code>Please enter the size of A: 3
Please enter A: 1 2 3
Please enter the number N: 3
Subsets: 
{ }
{ 1 }
{ 1 2 }
{ 1 2 3 }
{ 2 3 }
{ 2 3 }
{ 2 }
{ 1 2 }
</code></pre>

<p>this is the correct number of subsets (2^size) but as you can see it prints a few duplicates and not some of the subsets.</p>

<p>Any ideas where I am going wrong in my code?</p>

<pre><code>import java.util.Scanner;

public class subSetGenerator 
{   
    // Fill an array with 0's and 1's
    public static int [] fillArray(int [] set, int size)
    {
        int[] answer;
        answer = new int[20];

        // Initialize all elements to 1
        for (int i = 0; i &lt; answer.length; i++)
            answer[i] = 1;

        for (int a = 0; a &lt; set.length; a++)
            if (set[a] &gt; 0)
                answer[a] = 0;  

        return answer;
    } // end fill array

    // Generate a mask
    public static void maskMaker(int [] binarySet, int [] set, int n, int size)
    {
        int carry;
        int count = 0;
        boolean done = false;

        if (binarySet[0] == 0)
            carry = 0;

        else
            carry = 1;

        int answer = (int) Math.pow(2, size);

        for (int i = 0; i &lt; answer - 1; i++)
        {
            if (count == answer - 1)
            {
                done = true;
                break;
            }

            if (i == size)
                i = 0;

            if (binarySet[i] == 1 &amp;&amp; carry == 1)
            {
                binarySet[i] = 0;
                carry = 0;
                count++;
            } // end if

            else
            {
                binarySet[i] = 1;
                carry = 1;
                count++;
                //break;
            } // end else

            //print the set
            System.out.print("{ ");

            for (int k = 0; k &lt; size; k++)
                if (binarySet[k] == 1)
                    System.out.print(set[k] + " ");             

            System.out.println("}");

        } // end for

    } // maskMaker

    public static void main (String args [])
    {
        Scanner scan = new Scanner(System.in);
        int[] set;           
        set = new int[20];
        int size = 0;  
        int n = 0;

        // take input for A and B set
        System.out.print("Please enter the size of A: ");
        size = scan.nextInt();

        if (size &gt; 0)
        {
            System.out.print("Please enter A: ");
            for (int i = 0; i &lt; size; i++)
                set[i] = scan.nextInt();
        } // end if

        System.out.print("Please enter the number N: ");
        n = scan.nextInt();

        //System.out.println("Subsets with sum " + n + ": ");
        System.out.println("Subsets: ");

        System.out.println("{ }");
        maskMaker(fillArray(set, size), set, n, size);

    } // end main

}  // end class
</code></pre>
