Advertisement
mmayoub

School, 07.09.2017, Ex3, print common items in two arrays

Sep 7th, 2017
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.68 KB | None | 0 0
  1.  
  2. public class Ex3 {
  3.     // Write a Java program to find the common elements between two arrays of
  4.     // integers
  5.     public static void main(String[] args) {
  6.         int[] arr1 = { 1, 3, 7, 9, 15, 20, 6 };
  7.         int[] arr2 = { 7, 9, 5, 6, 3, 2 };
  8.         // output: 7, 9, 6, 3
  9.  
  10.         for (int i = 0; i < arr1.length; i += 1) {
  11.             if (isExist(arr1[i], arr2))
  12.                 System.out.print(arr1[i] + " ");
  13.  
  14.             // for (int j = 0; j < arr2.length; j += 1) {
  15.             // if (arr1[i] == arr2[j])
  16.             // System.out.print(arr1[i] + " ");
  17.             // }
  18.         }
  19.  
  20.     }
  21.  
  22.     private static boolean isExist(int x, int[] arr) {
  23.         for (int i = 0; i < arr.length; i += 1) {
  24.             if (arr[i] == x)
  25.                 return true;
  26.         }
  27.         return false;
  28.     }
  29.  
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement