title: Preventing a loop within a loop. How to reduce complexity
body: <p>You have 2 arrays.<br>
  arrA is empty.<br>
  arrB is full of stuff (what it's full of doesnt matter, but assume it's huge).  </p>
  
  <p>When a user does a thing, an item is removed from arrB and that item is placed in arrA.</p>
  
  <p>When a user does a different thing, it pulls items from arrA and puts it in arrB. </p>
  
  <p>Is it possible to do this without having a loop within a loop?  </p>
  
  <p>Or to put it in computer science terminology:<br>
  Is it possible to do this with linear ( ϴ(n) ) time/space complexity?<br>
  Right now I have something that is at least ϴ(n*k) </p>
  
  <p>(where n is the length of arrB, and k is the number of items passed to applyItems):</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var arrA = [], arrB = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
  
  function addToArray(arrayToAddTo, item){
    if(arrayToAddTo.indexOf(item) === -1){
      arrayToAddTo.push(item);
    }
  }
  
  function removeFromArray(arrayToRemoveFrom, item){
    var i = arrayToRemoveFrom.length;
    var temp = [];
    while(i--){
      if(arrayToRemoveFrom[i] !== item){
        temp.push(arrayToRemoveFrom[i]);
      }   
    }
    return temp;
  }
  
  function applyItems(arrayOfItems){
    var i = arrayOfItems.length;
    while(i--){
      var current = arrayOfItems[i]
      addToArray(arrA, current);
      arrB = removeFromArray(arrB, current);
    }
  }
  
  applyItems([0, 5, 3]);
  console.log(arrA);
  console.log(arrB);</code></pre>
  </div>
  </div>
  </p>
  
  <p>applyItems works, but is not efficient. </p>
  
  <p>Is it possible to decrease the time/space complexity here? </p>