Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Ex3 {
- // Write a Java program to find the common elements between two arrays of
- // integers
- public static void main(String[] args) {
- int[] arr1 = { 1, 3, 7, 9, 15, 20, 6 };
- int[] arr2 = { 7, 9, 5, 6, 3, 2 };
- // output: 7, 9, 6, 3
- for (int i = 0; i < arr1.length; i += 1) {
- if (isExist(arr1[i], arr2))
- System.out.print(arr1[i] + " ");
- // for (int j = 0; j < arr2.length; j += 1) {
- // if (arr1[i] == arr2[j])
- // System.out.print(arr1[i] + " ");
- // }
- }
- }
- private static boolean isExist(int x, int[] arr) {
- for (int i = 0; i < arr.length; i += 1) {
- if (arr[i] == x)
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement