I have an assignment that requires that I create two arrays (with user defined length) full of random numbers between 1-100, then merge the two arrays without duplicates in alternating order. I just need help merging the two without any repeating numbers. Can anyone help? Thanks!
import java.util.Scanner;
import java.lang.Math;
class Main {
public static void main(String[] args){
Scanner scan = new Scanner (System.in);
int f=0;
int j=0;
int k=0;
//getting first array length and making array1 and array2 that length
while (f==0){
System.out.println("Enter an array length (must be 10 or greater):");
int length = scan.nextInt();
if (length < 10){
f=0;
}
else{
f=1;
}
if (f==1){
int [] array1 = new int[length];
int [] array2 = new int[length];
int [] array3 = new int[length*2];
System.out.print("First Array: ");
//creating random integers between 1 and 100 inclusive to fill array1
for (int i=0; i<=length-1; i++){
int x = (int)(Math.random()*100)+1;
array1[i] = x;
System.out.print(x+" ");
}
//creating random integers between 1 and 100 inclusive to fill array2
System.out.print("\nSecond Array: ");
for (int i=0; i<=length-1; i++){
int y = (int)(Math.random()*100)+1;
System.out.print(y+" ");
array2[i] = y;
}
//combining both arrays
System.out.print("\nMerged Array: ");
for (int i=0; i<=length*2-1; i++){
if ((i==0) || (i%2==0)){
array3[i] = array1[j];
j++;
}
else{
array3[i] = array2[k];
k++;
}
System.out.print(array3[i]+" ");
}
}
}
}
}