Merge two arrays

Run Settings
LanguageJava
Language Version
Run Command
import java.util.Arrays; class Main { /* Write a function or program that takes two sorted arrays of integers as input and returns a single sorted array containing all the elements from both input arrays. Array 1: [1, 3, 5, 7] Array 2: [2, 4, 6, 8] */ //create new array1 and array2 // merge the size of the length of both new arrays // copy elements of array1 into a new array // copy elemets of array2 into a new array starting from the length of array1 // return merged array public static int[] mergeArray(int[] array1, int[] array2){ int[] mergedArray = new int[array1.length + array2.length]; System.arraycopy(array1,0, mergedArray,0, array1.length); System.arraycopy(array2,0, mergedArray,array1.length, array2.length); Arrays.sort(mergedArray); return mergedArray; } public static void main(String[] args) { int[] array1 = {1,3,5,7}; int[] array2 = {2,4,6,8}; int[] output = mergeArray(array1, array2); System.out.print(Arrays.toString(output)); } }
Editor Settings
Theme
Key bindings
Full width
Lines