I am trying to merge 2 arrays in this way:
int[] arr1 = { 1, 3, 9, 5 };
int[] arr2 = { 7, 0, 5, 4, 3 };
now I need to create a new array that looks like this:
int[] merged = { 1, 3, 9, 5, 7, 0, 4 };
so I need to put all the numbers in the new array but if a number is in both arrays, then it shouldn't duplicate, every number should be only once in the merged
array.
A number will never be twice or more in the same array.
this is the program that I built:
int[] arr1 = { 1, 6, -6, -9, 3, 4, -8, -7 };
int[] arr2 = { 5, 3, 2, 1, 70, 6, 7, -9, 99, 81 };
int counter = arr1.length;
boolean b = true;
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2.length && b == true; j++) {
if (arr1[i] == arr2[j])
b = false;
}
if (b == true) {
counter++;
}
b = true;
}
System.out.println(counter);
for some reason it doesn't work..
I am trying to write this program without built-in functions or lists, thank you.