title: Trying to solve symmetric difference using Javascript
body: <p>I am trying to figure out a solution for symmetric 
  difference using javascript that accomplishes the following
  objectives:</p>
  
  <ul>
  <li>accepts an unspecified number of arrays as arguments</li>
  <li>preserves the original order of the numbers in the arrays</li>
  <li>does not remove duplicates of numbers in single arrays</li>
  <li>removes duplicates occurring across arrays</li>
  </ul>
  
  <p>Thus, for example,
  if the input is ([1, 1, 2, 6], [2, 3, 5], [2, 3, 4]),
  the solution would be, [1, 1, 6, 5, 4].</p>
  
  <p>I am trying to solve this as challenge given by an online
  coding community. The exact instructions of the challenge
  state,</p>
  
  <blockquote>
    <p>Create a function that takes two or more arrays and returns an array
    of the symmetric difference of the provided arrays.</p>
    
    <p>The mathematical term symmetric difference refers to the elements in
    two sets that are in either the first or second set, but not in both.</p>
  </blockquote>
  
  <p>Although my solution below finds the numbers that are
  unique to each array, it eliminates all numbers occuring
  more than once and does not keep the order of the numbers. </p>
  
  <p>My question is very close to the one asked at <a href="https://stackoverflow.com/questions/30468692/finding-symmetric-difference-unique-elements-in-multiple-arrays-in-javascript">finding symmetric difference/unique elements in multiple arrays in javascript</a>.   However, the solution
  does not preserve the original order of the numbers and does not preserve duplicates of unique numbers occurring in single arrays. </p>
  
  <pre><code>function sym(args){
      var arr = [];
      var result = [];
      var units;
      var index = {};
      for(var i in arguments){
          units = arguments[i];
  
      for(var j = 0; j &lt; units.length; j++){
           arr.push(units[j]);
          }
      }
  
      arr.forEach(function(a){
          if(!index[a]){
              index[a] = 0;
          }
              index[a]++;
  
      });
  
         for(var l in index){
             if(index[l] === 1){
                 result.push(+l);
             }
         }
  
      return result;
  }
  symsym([1, 1, 2, 6], [2, 3, 5], [2, 3, 4]); // =&gt; Desired answer: [1, 1, 6. 5. 4]
  </code></pre>