Facebook
From NoamDadon, 3 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 47
  1. package MyPac;
  2.  
  3. public class Sort {
  4.  
  5.     public static int[] randomGenerator(int[]arr) {
  6.         int temp[] = new int[arr.length];
  7.         for(int i = 0; i < arr.length; i+=1){
  8.             temp[i] = (int) (Math.random() * 100);
  9.         }
  10.         return temp;
  11.     }
  12.  
  13.     public static int[] compareArr(int[] arr1, int[] arr2) {
  14.         int[] temp = new int[arr1.length + arr2.length];
  15.         for (int i = 0; i < arr1.length; i += 1) {
  16.             temp[i] = arr1[i];
  17.         }
  18.  
  19.         for (int i = 0; i < arr2.length; i += 1) {
  20.             temp[arr1.length + i] = arr2[i];
  21.         }
  22.         System.out.println();
  23.  
  24.         /*compare the last 3 digits(the size of the second array) with the first 5 (the size of the first array)
  25.         eliminate any duplicate in the array*/
  26.         for(int i = 0; i < arr2.length; i +=1){
  27.             for (int k = 0; k < arr1.length; k+=1){
  28.                 if(temp[i + arr1.length] == temp[k]){
  29.                     temp[i + arr1.length] = -1;
  30.                     temp[k] = -1;
  31.                 }
  32.             }
  33.         }
  34.         return temp;
  35.     }
  36. }
  37.  
  38. ========================================================================================================================
  39.  
  40. package MyPac;
  41.  
  42. public class Tester {
  43.     public static void main(String[] args) {
  44.         int[] arr1 = new int[5];
  45.         int[] arr2 = new int[3];
  46.         int[] arr3;
  47.  
  48.         arr1 = Sort.randomGenerator(arr1);
  49.         arr2 = Sort.randomGenerator(arr2);
  50.         arr3 = Sort.compareArr(arr1, arr2);
  51.  
  52.         for(int i:arr3){
  53.             System.out.print(i+" | ");
  54.         }
  55.     }
  56. }