Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.lang.Math;
- public class Thanos {
- public static void main(String str[]) {
- Scanner scan = new Scanner(System.in);
- // Input IVAR
- int input = 0;
- // Query For Array Length
- while (input < 10) {
- System.out.println("Enter an array length (must be 10 or greater):");
- input = scan.nextInt();
- }
- // Create 1st and Second Array, Fill them with random numbers inclusive 1 - 100.
- int[] array = new int[input];
- int[] array2 = new int[input];
- for (int i = 0; i < array.length; i++) {
- array[i] = (int) ((Math.random() * 100) + 1);
- array2[i] = (int) ((Math.random() * 100) + 1);
- }
- // Create Merged Array
- int[] array3 = new int[input * 2];
- // Primary IVARS
- boolean flag = true;
- // Counting Variables to keep track of index of 1st and 2nd arrays
- int count1 = 0;
- int count2 = 0;
- // Merged arrays one after the other, if its a duplicate - replace with zero and move on
- for(int i = 0;i < array3.length;i++){
- if (flag){
- boolean dup = false;
- for (int b = 0; b < array3.length;b++){
- // Check For Duplicates, Print Zero if so
- if (array[count1] == array3[b]){
- dup = true;
- array3[i] = 0;
- count1++;
- break;
- }
- }
- // If not a duplicate, go ahead and put number in array
- if(!dup){
- array3[i] = array[count1];
- count1++;
- }
- // Flag variable will make for-loop go every other order
- flag = false;
- }
- else if (!flag){
- boolean dup2 = false;
- // Same as above
- for (int b = 0; b < array3.length;b++){
- if (array2[count2] == array3[b]){
- dup2 = true;
- count2++;
- array3[i] = 0;
- break;
- }
- }
- if(!dup2){
- array3[i] = array2[count2];
- count2++;
- }
- flag = true;
- }
- }
- // Print 1st and 2nd Array
- System.out.println();
- System.out.print("First Array: ");
- for(int i = 0; i < array.length;i++){
- System.out.print(array[i] + " ");
- }
- System.out.println();
- System.out.println();
- System.out.print("Second Array: ");
- for(int i = 0; i < array2.length;i++){
- System.out.print(array2[i] + " ");
- }
- System.out.println();
- System.out.println();
- // Print merged array, EXCEPT IF ITS IS 0, Skip to the next one if so
- System.out.print("Merged Array: ");
- for (int i = 0; i < array3.length;i++){
- if (!(array3[i] == 0)){
- System.out.print(array3[i] + " ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement