title: How to Find Union of Two String Arrays
body: <p>I am trying to find the union of two string arrays.  I have created a new array and have copied all the data from the first set into the new array.  I am having trouble adding the information from the second set into the new array.  </p>
  
  <p>I need to use loops to search the second array and find the duplicates.  I keep getting an <code>ArrayIndexOutOfBoundsException</code>.</p>
  
  <p>Here is my current code:</p>
  
  <pre><code>static String[] union(String[] set1, String[] set2) {
      String union[] = new String[set1.length + set2.length];
  
      int i = 0;
      int cnt = 0;
  
      for (int n = 0; n &lt; set1.length; n++) {
          union[i] = set1[i];
          i++;
          cnt++;
      }
  
      for (int m = 0; m &lt; set2.length; m++) {
          for (int p = 0; p &lt; union.length; p++) {
              if (set2[m] != union[p]) {
                  union[i] = set2[m];
                  i++;
              }
          }
      }
      cnt++;
  
      union = downSize(union, cnt);
  
      return union;
  }
  </code></pre>