title: mergeTwo CodingBat puzzle in Java
body: <p>I am trying to solve <a href="http://codingbat.com/prob/p139150" rel="nofollow noreferrer">this</a> CodingBat problem: </p>
  
  <blockquote>
    <p>Start with two arrays of strings, A and B, each with its elements in alphabetical order and without duplicates. Return a new array containing the first N elements from the two arrays. The result array should be in alphabetical order and without duplicates. A and B will both have a length which is N or more. The best "linear" solution makes a single pass over A and B, taking advantage of the fact that they are in alphabetical order, copying elements directly to the new array. </p>
  </blockquote>
  
  <pre><code>mergeTwo({"a", "c", "z"}, {"b", "f", "z"}, 3) → {"a", "b", "c"}
  mergeTwo({"a", "c", "z"}, {"c", "f", "z"}, 3) → {"a", "c", "f"}
  mergeTwo({"f", "g", "z"}, {"c", "f", "g"}, 3) → {"c", "f", "g"}
  </code></pre>
  
  <p>Attempt: </p>
  
  <pre><code>public String[] mergeTwo(String[] a, String[] b, int n) {
      String[] op = new String[a.length + b.length];
      for (int i = 0; i &lt; a.length; i++) {
          op[i] = a[i];
      }
      int j = 0;
      for (int i = 0; i &lt; op.length; i++) {
          if (op[i] == null) {
              j = i;
              break;
          }
      }
      for (int i = 0; i &lt; b.length; i++) {
          op[j] = b[i];
          j++;
      }
      Arrays.sort(op);
      ArrayList&lt;String&gt; help = new ArrayList&lt;String&gt;();
  
      for (int i = 0; i &lt; op.length - 1; i++) {
          if (op[i] != op[i + 1]) {
              help.add(op[i]);
          }
      }
      String[] res = new String[help.size()];
      for (int i = 0; i &lt; help.size(); i++) {
          res[i] = help.get(i);
      }
      String[] op2 = new String[n];
      for (int i = 0; i &lt; n; i++) {
          op2[i] = res[i];
      }
      return op2;
  
  }
  </code></pre>
  
  <p>So the problem is that all the tests pass except one: </p>
  
  <p><a href="https://i.stack.imgur.com/yp4Mu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yp4Mu.png" alt="enter image description here"></a></p>
  
  <p>Why is that?</p>