title: Two nested for/loops without duplicated pairs
body: <p>We have array:</p>
  
  <pre><code>var arr = [0,1,2];
  </code></pre>
  
  <p>and loops:</p>
  
  <pre><code>for ( var i=0; i&lt;arr.length; i++ ) {
      for ( var j=0; j&lt;arr.length; j++ ) {
          if ( arr[i] == arr[j] ) continue;
          console.log(arr[i], arr[j]);
      }
  }
  </code></pre>
  
  <p>and output:</p>
  
  <pre><code>0,1 //a--|
  0,2 //b--|--|
  1,0 //a--|  |
  1,2 //c-----|--|
  2,0 //b-----|  |
  2,1 //c--------|
  </code></pre>
  
  <p>as we can see there are duplicated pairs (in comments a, b and c occurs twice)</p>
  
  <p>the only way we can get rid of duplicated pairs is to store already "matched" pairs in some sort of memory?</p>
  
  <pre><code>var mem = {};
  
  for ( var i=0; i&lt;arr.length; i++ ) {
      for ( var j=0; j&lt;arr.length; j++ ) {
          var left = i;
          var right = j;
          if ( left &gt; right ) {
              var temp = right;
              right = left;
              left = temp;
          }
  
          if ( arr[i] == arr[j] || mem[arr[left]+","+arr[right]] ) continue;
  
          console.log(arr[i], arr[j]);
          mem[arr[left]+","+arr[right]] = true;
      }
  }
  </code></pre>
  
  <p>and output:</p>
  
  <pre><code>0,1
  0,2
  1,2
  </code></pre>
  
  <p>but this takes additional memory (for bigger arrays it takes a lot of it..)</p>
  
  <p>is there any other way without storing anything additional?</p>