title: Get all possible combination of items in array without duplicate groups in Swift
body: <p>I'm trying to create an extension on Array where I can get all possible combinations of an array without generating duplicate groups, including a no item combination. </p>
  
  <p>For example, for this array: </p>
  
  <pre><code>[1, 2, 3, 4]
  </code></pre>
  
  <p>The following possible combinations should be generated: </p>
  
  <pre><code>[[], [1], [2], [3], [4], [1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4], [1, 2, 3, 4]]
  </code></pre>
  
  <p>Please note that none of the groups repeat themselves, i.e: if there is a group [1, 2] there is no other group: [2, 1].</p>
  
  <p>This is the closest I've been able to get to a result: </p>
  
  <pre><code>public extension Array {
  
  func allPossibleCombinations() -&gt; [[Element]] {
      var output: [[Element]] = [[]]
      for groupSize in 1...self.count {
          for (index1, item1) in self.enumerated() {
              var group = [item1]
              for (index2, item2) in self.enumerated() {
                  if group.count &lt; groupSize {
                      if index2 &gt; index1 {
                          group.append(item2)
                          if group.count == groupSize {
                              output.append(group)
                              group = [item1]
                              continue
                          }
                      }
                  } else {
                      break
                  }
              }
              if group.count == groupSize {
                  output.append(group)
              }
          }
      }
      return output
  }
  
  }
  </code></pre>
  
  <p>But it is missing possible combination of items in the group size 3 (I only get back <code>[1, 2, 3]</code> and <code>[2, 3, 4]</code>.</p>
  
  <p>Much appreciated!</p>