title: All possible pairs
body: <p>Given a set of numbers, from 1 to n, I need to model all sets of all possible pairs. </p>
  
  <p>In other words, one drawed set consists of all numbers. Numbers are paired. If the count is odd - one entry with one number is allowed (in fact it's required). Sets need to be unique, i.e. pair [1,2] is the same as [2,1] (edited: and solution: [1,2] [3,4] is the same as [3,4] [1,2]).</p>
  
  <p>For instance, when n equals 5, following sets can be created:</p>
  
  <pre><code>[1,2] [3,4] [5]
  [1,2] [3,5] [4]
  [1,2] [4,5] [3]
  [1,3] [2,4] [5]
  [1,3] [2,5] [4]
  [1,3] [4,5] [2]
  ....
  </code></pre>
  
  <p>I am almost able to model the solution, but the uniqueness constraint is hard for me to implement. </p>
  
  <p>Moreover - I feel like my solution lacks in performance. I now that the problem space is big, but for me even for n=12 computation takes more than 5 minutes.</p>
  
  <pre><code>public void compute(HashSet&lt;Number&gt; numbers, ArrayList&lt;Pair&gt; pairs) {
      if (numbers.size() &lt;= 1) {
          print(pairs);
      } else {
          for (Number number1 : numbers) {
              for (Number number2 : numbers) {
                  if (number1 != number2) {
                      Set&lt;Number&gt; possibleNumbers = new HashSet&lt;Number&gt;(numbers);
                      List&lt;Pair&gt; allPairs = new ArrayList&lt;Pair&gt;(pairs);
  
                      possibleNumbers.remove(number1);
                      possibleNumbers.remove(number2);
                      allPairs.add(new Pair(number1, number2));
  
                      compute(possibleNumbers, allPairs);
                  }
              }
          }
      }
  }
  
  compute(numbers, new ArrayList&lt;Pair&gt;());
  </code></pre>
  
  <p>For n=3 I get doubled set of solutions:</p>
  
  <pre><code>[0 1] [2] 
  [0 2] [1] 
  [1 0] [2] 
  [1 2] [0] 
  [2 0] [1] 
  [2 1] [0]
  </code></pre>
  
  <p>So: how should I implement the problem to get rid of the duplicates. How can I improve the implementation to speed up the processing?</p>