Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.lang.Math;
  3. public class Thanos {
  4. public static void main(String str[]) {
  5. Scanner scan = new Scanner(System.in);
  6. // Input IVAR
  7. int input = 0;
  8. // Query For Array Length
  9. while (input < 10) {
  10. System.out.println("Enter an array length (must be 10 or greater):");
  11. input = scan.nextInt();
  12. }
  13. // Create 1st and Second Array, Fill them with random numbers inclusive 1 - 100.
  14. int[] array = new int[input];
  15. int[] array2 = new int[input];
  16. for (int i = 0; i < array.length; i++) {
  17. array[i] = (int) ((Math.random() * 100) + 1);
  18. array2[i] = (int) ((Math.random() * 100) + 1);
  19. }
  20. // Create Merged Array
  21. int[] array3 = new int[input * 2];
  22. // Primary IVARS
  23. boolean flag = true;
  24. // Counting Variables to keep track of index of 1st and 2nd arrays
  25. int count1 = 0;
  26. int count2 = 0;
  27. // Merged arrays one after the other, if its a duplicate - replace with zero and move on
  28. for(int i = 0;i < array3.length;i++){
  29. if (flag){
  30. boolean dup = false;
  31. for (int b = 0; b < array3.length;b++){
  32. // Check For Duplicates, Print Zero if so
  33. if (array[count1] == array3[b]){
  34. dup = true;
  35. array3[i] = 0;
  36. count1++;
  37. break;
  38. }
  39. }
  40. // If not a duplicate, go ahead and put number in array
  41. if(!dup){
  42. array3[i] = array[count1];
  43. count1++;
  44. }
  45. // Flag variable will make for-loop go every other order
  46. flag = false;
  47. }
  48. else if (!flag){
  49. boolean dup2 = false;
  50. // Same as above
  51. for (int b = 0; b < array3.length;b++){
  52. if (array2[count2] == array3[b]){
  53. dup2 = true;
  54. count2++;
  55. array3[i] = 0;
  56. break;
  57. }
  58. }
  59. if(!dup2){
  60. array3[i] = array2[count2];
  61. count2++;
  62. }
  63. flag = true;
  64. }
  65. }
  66.  
  67. // Print 1st and 2nd Array
  68. System.out.println();
  69. System.out.print("First Array: ");
  70. for(int i = 0; i < array.length;i++){
  71. System.out.print(array[i] + " ");
  72. }
  73. System.out.println();
  74. System.out.println();
  75. System.out.print("Second Array: ");
  76. for(int i = 0; i < array2.length;i++){
  77. System.out.print(array2[i] + " ");
  78. }
  79. System.out.println();
  80. System.out.println();
  81. // Print merged array, EXCEPT IF ITS IS 0, Skip to the next one if so
  82. System.out.print("Merged Array: ");
  83. for (int i = 0; i < array3.length;i++){
  84. if (!(array3[i] == 0)){
  85. System.out.print(array3[i] + " ");
  86. }
  87. }
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement