An Awesome program: Common elements in two arrays
Program: Common elements in two arrays: solved Java program for ICSE Class 10 Computer Applications with complete BlueJ code, explanation and sample output.
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
JAVA videos
Related Posts
Continue learning with these related articles
Sum of First n Natural Numbers in Java (Loop and Formula)
Find the sum of the first n natural numbers in Java with a for loop, a while loop and the formula n(n + 1)/2, with a dry run and the sums of even numbers, odd numbers, squares and cubes.
Prime Number Program in Java: Check a Number and Print Primes in a Range
Check whether a number is prime in Java by counting its factors, a faster square-root method with isPrime(), and a program that prints all prime numbers in a range.

Encapsulation, Access Specifiers and Scope of Variables in Java for ICSE Class 10
ICSE Class 10 Computer Applications notes on encapsulation: private, default, protected and public access, data hiding with getter and setter methods, instance vs static (class) variables, local variables and scope, with a solved BankAccount program.