public int[] merge(int[] array1, int[] array2) {
    validateArrays(array1, array2);

    int[] result = new int[array1.length + array2.length];

    int i = 0, j = 0, k = 0;
    while (i < array1.length && j < array2.length) {
      if (array1[i] < array2[j]) {
        result[k++] = array1[i++];
      } else {
        result[k++] = array2[j++];
      }
    }

    while (i < array1.length) {
      result[k++] = array1[i++];
    }
    while (j < array2.length) {
      result[k++] = array2[j++];
    }
    return result;
  }
The complexity order of this algorithm in space terms is O(N+M) where N is the length of the
first array and M is the length of the second array. In time terms the complexity order is
equivalent, because we are going to iterate over array1 and array2 to merge every element.
src/main/java/com/github/pedrovgs/problem6/MergeSortedArrays.java