In Java, WAP to store 5 numbers in one array then another 5 numbers in another array then create a new array to merge those two array keep the common values of both the array. Display the valued of merged array and also display how many such numbers found.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] one = {5, 6, 3, 4, 2};
int[] two = {4, 4, 7, 8, 2};
int[] three = new int[one.length + two.length];
int common = 0;
boolean same;
for (int i = 0, j = 0, k = 0; i < three.length / 2; i++, j++, k++) {
three[i] = one[j];
same = false;
for (int l = j - 1; l >= 0; l--) {
if (one[j] == one[l]) {
same = true;
break;
}
}
if (!same) {
for (int value : two) {
if (one[j] == value) {
common++;
break;
}
}
}
three[three.length / 2 + i] = two[k];
}
System.out.println(Arrays.toString(three));
System.out.println(common);
}
}
Comments
Leave a comment