title: Map/Set to maintain unique array of arrays, Javascript
body: <p>I am trying to build unique array of arrays such that whenever I have new array to add it should only add if it doesn't already exist in collection</p>
  <p>E.g. store all unique permutations of [1,1,2]</p>
  <p>Actual : <code>[[1,1,2],[1,2,1],[1,1,2],[1,2,1],[2,1,1],[2,1,1]]</code><br />
  Expected : <code>[[1,1,2],[1,2,1],[2,1,1]]</code></p>
  <p>Approaches I tried:</p>
  <ol>
  <li><strong>Array.Filter</strong>: Doesn't work because arrays are object and each value in <code>uniqueArrComparer</code> is a unique object reference to that array element.</li>
  </ol>
  <pre class="lang-js prettyprint-override"><code>function uniqueArrComparer(value, index, self) {
    return self.indexOf(value) === index;
  }
  
  result.filter(uniqueArrComparer)
  </code></pre>
  
  <ol start="2">
  <li><p><strong>Set/Map</strong>: Thought I can build a unique array set but it doesn't work because Set internally uses strict equality comparer (===), which will consider each array in this case as unique.<br />
  <a href="https://stackoverflow.com/q/29759480/351708" title="How to customize object equality for JavaScript Set">We cannot customize object equality for JavaScript Set</a></p>
  </li>
  <li><p>Store each array element as a string in a Set/Map/Array and build an array of unique strings. In the end build array of array using array of unique string. This approach will work but doesn't look like efficient solution.</p>
  </li>
  </ol>
  <h3>Working solution using Set</h3>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>let result = new Set();
  
  // Store [1,1,2] as "1,1,2"
  result.add(permutation.toString());
  
  return Array.from(result)
    .map(function(permutationStr) {
  
      return permutationStr
        .split(",")
        .map(function(value) {
  
          return parseInt(value, 10);
        });
    });</code></pre>
  </div>
  </div>
  </p>
  <p>This problem is more of a learning exercise than any application problem.</p>