Write a program to accept two integer arrays from the user and print all the common elements in two arrays.
String programs
import java.io.*;
class common {
public static void main()throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the size of the first array");
int n=Integer.parseInt(br.readLine());
int Arr1[]=new int[n];
System.out.println("Enter the values");
for(int i=0;i<n;i++)
Arr1[i]=Integer.parseInt(br.readLine());
System.out.println("Enter the size of the second array");
int o=Integer.parseInt(br.readLine());
int Arr2[]=new int[o];
System.out.println("Enter the values");
for(int i=0;i<o;i++)
Arr2[i]=Integer.parseInt(br.readLine());
boolean flag=false;
for(int i=0;i<n;i++)
{
for(int j=0;j<o;j++)
{
if(Arr1[i]==Arr2[j])
{
System.out.print("THE COMMON ELEMENTS ARE-"+Arr1[i]+"\t");
flag=true;
}
}
}
System.out.println();
if(flag==false)
System.out.println("THERE ARE NO COMMON ELEMENTS");
}
}
Output:
Enter the size of the first array
4
Enter the values
1
2
3
4
Enter the size of the second array
5
Enter the values
2
4
6
8
10
THE COMMON ELEMENTS ARE- 2
THE COMMON ELEMENTS ARE- 4