forked from CodeToExpress/dailycodebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Common2.java
61 lines (60 loc) · 1.54 KB
/
Common2.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication3;
/**
* @date 30/01/19
* @author SPREEHA DUTTA
*/
import java.util.*;
public class Common2 {
public static void findcommon(int a[],int b[])
{
int i=0,j=0;
while(true)
{
if(a[i]>b[j])
j++;
else if(a[i]<b[j])
i++;
else
{
if(i==0||j==0)
System.out.print(a[i]+" ");
if(i!=0 && a[i]!=a[i-1] && j!=0 && b[j]!=b[j-1])
{
System.out.print(a[i]+" ");
i++;
j++;
}
else
{
i++; j++;
}
}
if(i==a.length || j==b.length)
break;
}
}
public static void main(String []args)
{
int n,m,i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of first array ");
n=sc.nextInt();
int a[]=new int[n];
for(i=0;i<n;i++)
a[i]=sc.nextInt();
System.out.println("Enter size of second array ");
m=sc.nextInt();
int b[]=new int[m];
for(i=0;i<m;i++)
b[i]=sc.nextInt();
System.out.println("Common elements are ");
Arrays.sort(a);
Arrays.sort(b);
findcommon(a,b);
}
}