Advertisement
Guest User

solution

a guest
Aug 18th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.69 KB | None | 0 0
  1.     public static int[] intersectionOfTwoArrays(int[] firstArr, int[] secondArr) {
  2.  
  3.         if (firstArr.length > secondArr.length) {
  4.             return intersectArrays(firstArr, secondArr);
  5.         } else {
  6.             return intersectArrays(secondArr, firstArr);
  7.         }
  8.     }
  9.  
  10.     private static int[] intersectArrays(int[] firstArr, int[] secondArr) {
  11.         int[] result = new int[secondArr.length];
  12.         for (int i = 0; i < firstArr.length; i++) {
  13.             for (int j = 0; j < secondArr.length; j++) {
  14.                 if (firstArr[i] == secondArr[j]) {
  15.                     result[j] = secondArr[j];
  16.                 }
  17.             }
  18.         }
  19.  
  20.         return result;
  21.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement