Answer to Question #257510 in Java | JSP | JSF for Nikhil Sahoo

Question #257510

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.


1
Expert's answer
2021-10-29T11:55:25-0400
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);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS