title: Filtering non-"linear", progressive values from array
body: <p>I'm not exactly sure how to explain this, but bear with me...</p>
  
  <p>I have written a program that creates random arrays of lengths that range from 0-4 like so: <code>[]</code> or <code>[n]</code> or <code>[n,n]</code> or <code>[n,n,n]</code> or <code>[n,n,n,n]</code>.</p>
  
  <p>The input will be a random digit between 1-6, whereby the output, <code>n</code>, is always a multiple of the input. For example, the input might be <code>2</code> and the output might be <code>[2]</code> or <code>[4,8]</code> or <code>[2,4,6]</code>.</p>
  
  <p>The output will always be sorted from lowest to highest value.</p>
  
  <p>The input will always be the lowest multiple, whereas the highest multiple will always be the value of <code>input * 4</code>. For instance, if the input were <code>3</code>, the range of output would be 3-12 or 3, 6, 9, and 12.</p>
  
  <p>Hypothetically, let's say the input is <code>4</code>, whereas the output, if any, is of course multiples of 4.</p>
  
  <p>The only possible outputs are...</p>
  
  <p><strong><code>[]</code></strong>
  <strong><code>[4]</code></strong>
  <code>[8]</code>
  <code>[12]</code>
  <code>[16]</code>
  <strong><code>[4,8]</code></strong>
  <code>[4,12]</code>
  <code>[4,16]</code>
  <code>[8,12]</code>
  <code>[8,16]</code>
  <code>[12,16]</code>
  <strong><code>[4,8,12]</code></strong>
  <code>[4,8,16]</code>
  <code>[4,12,16]</code>
  <code>[8,12,16]</code>
  <strong><code>[4,8,12,16]</code></strong></p>
  
  <p>What I wanted to do was filter the output so that only the bold arrays above were valid. In addition to that, I wanted to preserve any valid array elements while removing all non-"linear", progressive elements like so:</p>
  
  <ul>
  <li><code>[4,12]</code> would become <strong><code>[4]</code></strong></li>
  <li><code>[4,8,16]</code> would become <strong><code>[4,8]</code></strong></li>
  <li><code>[8,16]</code> would become <strong><code>[]</code></strong></li>
  <li><code>[8,12,16]</code> would become <strong><code>[]</code></strong></li>
  <li><code>[12,16]</code> would become <strong><code>[]</code></strong></li>
  </ul>
  
  <p>I ended up finally figuring out how I could achieve this, but for whatever reason it took me a while to wrap my head around it; so now I'm just super curious. Is there another way to achieve this output which is definitively more efficient than the following code? Is it possible to do the same thing with some sort of loop? Are there any known algorithms out there that deal with this sort of thing?</p>
  
  <pre><code>    if (a[0] != 4) {
        a = [];
      } else if (a.length === 2 &amp;&amp; a[1] != 8) {
        a.pop();
      } else if (a.length === 3 &amp;&amp; a[1] != 8) {
        a.pop();
        a.pop();
      } else if (a.length === 3 &amp;&amp; a[2] != 12){
        a.pop();
      }
  </code></pre>