title: Subtracting one arrayList from another arrayList
body: <p>I have two arrayLists and I am trying to "subtract" one arrayList from another. For example, if I have one arrayList [1,2,3] and I am trying to subtract [0, 2, 4] the resulting arrayList should be [1,3]. </p>
  
  <pre><code>List&lt;Integer&gt; a = new ArrayList&lt;&gt;(Arrays.asList(1, 2, 3));
  List&lt;Integer&gt; b = Arrays.asList(0, 2, 4);
  subtract(a,b) // should return [1,3]
  </code></pre>
  
  <p>Here is my code.</p>
  
  <pre><code>//returns a new IntSet after subtracting a from b
  // .minus().toString()
  ArrayList&lt;Integer&gt; minusArray = new ArrayList&lt;Integer&gt;();
  
      minusArray.addAll(array1);
  
      for(int i =0; i&lt; minusArray.size(); i++){
          for(int j = 0; j &lt; array2.size(); j++){
              if(minusArray.get(i).equals(array2.get(j))){
                  minusArray.remove(i);
                  if(i == 0){
                      ;
                  }
                  else if(j == 0){
                      ;
                  }
                  else{
                      i = 0;
                      j = 0;
                  }
              }
              else{}
          }
      }
  
  return minusArray;
  </code></pre>
  
  <p>My code works in some cases, like if <code>arrayList1 = [4,6]</code> and <code>arrayList2 = [6]</code> it will will give me a result of <code>[4]</code>. But if I try something like <code>[1,2,4]</code> and <code>[0,4,8]</code></p>
  
  <p>I get this exception:</p>
  
  <pre><code>java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
      at java.util.ArrayList.rangeCheck(Unknown Source)
      at java.util.ArrayList.get(Unknown Source)
      at IntSet.minus(IntSet.java:119)
      at IntSetDriver.main(IntSetDriver.java:62)
  </code></pre>
  
  <p>Here is the code I have come up with. I have done test runs through it and to me I think it should work. The user inputs these arrayLists and they are presorted, I also do not know Hash or big-O. </p>
  
  <pre><code>ArrayList&lt;Integer&gt; minusArray = new ArrayList&lt;Integer&gt;();
  
      minusArray.addAll(array1);
  
      for(int i =0; i&lt; minusArray.size(); i++){
          for(int j = 0; j &lt; array2.size(); j++){
              if(minusArray.get(i).equals(array2.get(j))){
                  minusArray.remove(i);
              }
              else{}
          }
      }
  
  return minusArray;
  </code></pre>