-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommonElement.java
More file actions
73 lines (55 loc) · 1.71 KB
/
commonElement.java
File metadata and controls
73 lines (55 loc) · 1.71 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Write the program to find the common element in array both having same or different
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class commonElement {
public static void main(String[] args)throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the size of the array1: ");
int size1 = Integer.parseInt(br.readLine());
int a1[] = new int[size1];
System.out.println("Enter the array1 element: ");
for(int i=0;i<size1;i++)
{
a1[i] = Integer.parseInt(br.readLine());
}
System.out.println("Enter the size of the array1: ");
int size2 = Integer.parseInt(br.readLine());
int a2[] = new int[size2];
System.out.println("Enter the array2 element: ");
for(int i=0;i<size2;i++)
{
a2[i] = Integer.parseInt(br.readLine());
}
System.out.println("The common elements are: ");
if(size1 > size2)
{
int x=0;
while(x < size2)
{
for(int i=0;i<size1;i++)
{
if(a2[x]==a1[i])
{
System.out.println(a1[i]);
}
}
x++;
}
}
else
{
int x=0;
while(x < size1)
{
for(int i=0;i<size2;i++)
{
if(a1[x]==a2[i])
{
System.out.println(a1[i]);
}
}
x++;
}
}
}
}