@Test public void shouldMergeArraysWithCollisions() { int a1[] = { 1, 2, 3, 4, 9, 10, 11 }; int a2[] = { 5, 6, 7 }; int[] result = mergeSortedArrays.merge(a1, a2); int[] expectedArray = { 1, 2, 3, 4, 5, 6, 7, 9, 10, 11 }; assertArrayEquals(expectedArray, result); }
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; }
MergeSortedArrays { 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; } }
MergeSortedArrays { 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; }  }
MergeSortedArrays { 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; }  int[] merge(int[] array1, int[] array2); }
MergeSortedArrays { 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; }  int[] merge(int[] array1, int[] array2);  }