forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntersection_Of_TwoArrays.java
37 lines (36 loc) · 1.04 KB
/
Intersection_Of_TwoArrays.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package me.company;
import java.util.*;
public class Intersection_Of_TwoArrays {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
HashSet<Integer>s=new HashSet<Integer>();
System.out.println("Enter the no. of elements in array a");
int n1=sc.nextInt();
System.out.println("Enter the no. of elements in array b");
int n2=sc.nextInt();
int a[]=new int[n1];
int b[]=new int[n2];
System.out.println("Enter the elements of array a : ");
for(int i=0; i<n1; i++)
{
a[i]=sc.nextInt();
}
System.out.println("Enter the elements of array b : ");
for(int i=0; i<n2; i++)
{
b[i]=sc.nextInt();
}
for(int i=0; i<n1; i++)
s.add(a[i]);
int result=0;
for(int j=0; j<n2; j++)
{
if(s.contains(b[j])) {
result++;
s.remove(b[j]);
}
}
System.out.println("Required no. of common elements are : "+result);
}
}