// Program 48: Common Elements of Two Arrays import java.util.*; class CommonArrayElements { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Size of first array: "); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } System.out.print("Size of second array: "); int m = sc.nextInt(); int[] b = new int[m]; for (int i = 0; i < m; i++) { b[i] = sc.nextInt(); } int[] c = new int[n]; int count = 0; for (int i = 0; i < n; i++) { boolean common = false, repeated = false; for (int j = 0; j < m; j++) if (a[i] == b[j]) { common = true; } for (int k = 0; k < count; k++) if (c[k] == a[i]) { repeated = true; } if (common && !repeated) { c[count++] = a[i]; } } for (int i = 0; i < count - 1; i++) { for (int j = 0; j < count - i - 1; j++) { if (c[j] > c[j + 1]) { int t = c[j]; c[j] = c[j + 1]; c[j + 1] = t; } } } System.out.println("Common elements:"); for (int i = 0; i < count; i++) { System.out.print(c[i] + " "); } if (count == 0) { System.out.print("None"); } System.out.println(); } }