0

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]+" ");
    }
    }
    }
  }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
Climatee
  • 1
  • 2
  • Don't remove your code like that, the portion you left was not useful in isolation; feel free to edit though if you can make it more concise without removing everything. – Elliott Frisch Dec 21 '19 at 23:02
  • Sorry I'm new here! It just seems hard for someone to understand what's going on with all the code in their face. – Climatee Dec 21 '19 at 23:04
  • 1
    Does this answer your question? [Java - Merge Two Arrays without Duplicates (No libraries allowed)](https://stackoverflow.com/questions/35329907/java-merge-two-arrays-without-duplicates-no-libraries-allowed) – jmalenfant Dec 21 '19 at 23:05
  • It helps a bit but it's too complex for me to understand; there's a lot of notation not familiar to me. Since I am a beginner, I need to do this minimally with a for loop, but I'm not exactly sure how to. – Climatee Dec 21 '19 at 23:12

1 Answers1

0

First, let's extract your method to fill arrays.

static int[] fillRandomArray(int n) {
    int[] arr = new int[n];
    for (int i = 0; i < n; i++) {
        arr[i] = (int) (Math.random() * 100) + 1;
        System.out.print(arr[i] + " ");
    }
    System.out.println();
    return arr;
}

Now you can simplify your code to use that method, and your merge is very close; you don't need j or k in each case you are indexing by half of i (the cases being even or odd). Like,

Scanner scan = new Scanner(System.in);
while (true) {
    System.out.println("Enter an array length (must be 10 or greater):");
    int length = scan.nextInt();
    if (length >= 10) {
        System.out.print("First Array: ");
        // creating random integers between 1 and 100 inclusive to fill array1
        int[] array1 = fillRandomArray(length);
        // creating random integers between 1 and 100 inclusive to fill array2
        System.out.print("\nSecond Array: ");
        int[] array2 = fillRandomArray(length);
        // combining both arrays
        System.out.print("\nMerged Array: ");
        int[] array3 = new int[array1.length + array2.length];
        for (int i = 0; i < array3.length; i++) {
            if (i % 2 == 0) {
                array3[i] = array1[i / 2];
            } else {
                array3[i] = array2[i / 2];
            }
            System.out.print(array3[i] + " ");
        }
        System.out.println();
    }
}

If you actually need to eliminate duplicates between array1 and array2 while you merge, then you can't assume that the output array will be double the input length. I would use a Set. Like,

// combining both arrays
System.out.print("\nMerged Array: ");
Set<Integer> set = new LinkedHashSet<>();
for (int i = 0; i < array1.length + array2.length; i++) {
    if (i % 2 == 0) {
        if (set.add(array1[i / 2])) {
            System.out.print(array1[i / 2] + " ");
        }
    } else {
        if (set.add(array2[i / 2])) {
            System.out.print(array2[i] + " ");
        }
    }
}
// If you actually need an int[]
int[] array3 = set.stream().mapToInt(Integer::intValue).toArray();
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249