New course! Every coder should learn Generative AI!
Try a free lesson0
How can i merge two sorted array lists in order to produce one sorted array list? The inputs should be in ascending order .. lets say first array [ 2, 4,8,8,15 ] and the other one [ 4, 7, 8, 8, 8, 10] and return the merged array
3 Answers
+ 4
use join() and sort() methods?
+ 3
https://code.sololearn.com/cF5qe7lecgkK
public static void main(String[] args) {
int [] first = {2,4,8,8,15};
int [] second = {4, 7, 8, 8, 8, 10};
int [] merged = new int[first.length + second.length];
System.arraycopy(first, 0, merged, 0, first.length);
System.arraycopy(second, 0, merged, first.length, second.length);
Arrays.sort(merged);
System.out.println(Arrays.toString(merged));
}
output:
[2, 4, 4, 7, 8, 8, 8, 8, 8, 10, 15]
+ 1
Thanks 😁