Skip to content

Commit 9f5c3f6

Browse files
SpreehaMadhavBahl
authored andcommitted
day 22 (#225)
1 parent be14797 commit 9f5c3f6

File tree

3 files changed

+229
-0
lines changed

3 files changed

+229
-0
lines changed

day22/Java/Common2.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package javaapplication3;
7+
8+
/**
9+
* @date 30/01/19
10+
* @author SPREEHA DUTTA
11+
*/
12+
import java.util.*;
13+
public class Common2 {
14+
public static void findcommon(int a[],int b[])
15+
{
16+
int i=0,j=0;
17+
while(true)
18+
{
19+
if(a[i]>b[j])
20+
j++;
21+
else if(a[i]<b[j])
22+
i++;
23+
else
24+
{
25+
if(i==0||j==0)
26+
System.out.print(a[i]+" ");
27+
if(i!=0 && a[i]!=a[i-1] && j!=0 && b[j]!=b[j-1])
28+
{
29+
System.out.print(a[i]+" ");
30+
i++;
31+
j++;
32+
}
33+
else
34+
{
35+
i++; j++;
36+
}
37+
}
38+
if(i==a.length || j==b.length)
39+
break;
40+
}
41+
}
42+
public static void main(String []args)
43+
{
44+
int n,m,i;
45+
Scanner sc=new Scanner(System.in);
46+
System.out.println("Enter size of first array ");
47+
n=sc.nextInt();
48+
int a[]=new int[n];
49+
for(i=0;i<n;i++)
50+
a[i]=sc.nextInt();
51+
System.out.println("Enter size of second array ");
52+
m=sc.nextInt();
53+
int b[]=new int[m];
54+
for(i=0;i<m;i++)
55+
b[i]=sc.nextInt();
56+
System.out.println("Common elements are ");
57+
Arrays.sort(a);
58+
Arrays.sort(b);
59+
findcommon(a,b);
60+
}
61+
}

day22/Java/Common3.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package javaapplication3;
7+
8+
/**
9+
* @date 30/01/19
10+
* @author SPREEHA DUTTA
11+
*/
12+
import java.util.*;
13+
public class Common3 {
14+
public static void findcommon(int a[],int b[],int c[])
15+
{
16+
int i=0,j=0,k=0;
17+
while(true)
18+
{
19+
if(a[i]==b[j] && b[j]==c[k])
20+
System.out.print(a[i]+" ");
21+
if(a[i]<b[j])
22+
i++;
23+
else if(b[j]<c[k])
24+
j++;
25+
else
26+
k++;
27+
if(i==a.length || j==b.length || k==c.length)
28+
break;
29+
}
30+
}
31+
public static void main(String []args)
32+
{
33+
int n,m,k,i;
34+
Scanner sc=new Scanner(System.in);
35+
System.out.println("Enter size of first array ");
36+
n=sc.nextInt();
37+
int a[]=new int[n];
38+
for(i=0;i<n;i++)
39+
a[i]=sc.nextInt();
40+
System.out.println("Enter size of second array ");
41+
m=sc.nextInt();
42+
int b[]=new int[m];
43+
for(i=0;i<m;i++)
44+
b[i]=sc.nextInt();
45+
System.out.println("Enter size of third array ");
46+
k=sc.nextInt();
47+
int c[]=new int[k];
48+
for(i=0;i<k;i++)
49+
c[i]=sc.nextInt();
50+
System.out.println("Common elements are ");
51+
findcommon(a,b,c);
52+
}
53+
}

day22/README.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,68 @@ int main()
186186
return 0;
187187
}
188188
```
189+
190+
## Java Implementation
191+
192+
### [Solution](./Java/Common2.java)
193+
194+
```java
195+
/**
196+
* @date 30/01/19
197+
* @author SPREEHA DUTTA
198+
*/
199+
import java.util.*;
200+
public class Common2 {
201+
public static void findcommon(int a[],int b[])
202+
{
203+
int i=0,j=0;
204+
while(true)
205+
{
206+
if(a[i]>b[j])
207+
j++;
208+
else if(a[i]<b[j])
209+
i++;
210+
else
211+
{
212+
if(i==0||j==0)
213+
System.out.print(a[i]+" ");
214+
if(i!=0 && a[i]!=a[i-1] && j!=0 && b[j]!=b[j-1])
215+
{
216+
System.out.print(a[i]+" ");
217+
i++;
218+
j++;
219+
}
220+
else
221+
{
222+
i++; j++;
223+
}
224+
}
225+
if(i==a.length || j==b.length)
226+
break;
227+
}
228+
}
229+
public static void main(String []args)
230+
{
231+
int n,m,i;
232+
Scanner sc=new Scanner(System.in);
233+
System.out.println("Enter size of first array ");
234+
n=sc.nextInt();
235+
int a[]=new int[n];
236+
for(i=0;i<n;i++)
237+
a[i]=sc.nextInt();
238+
System.out.println("Enter size of second array ");
239+
m=sc.nextInt();
240+
int b[]=new int[m];
241+
for(i=0;i<m;i++)
242+
b[i]=sc.nextInt();
243+
System.out.println("Common elements are ");
244+
Arrays.sort(a);
245+
Arrays.sort(b);
246+
findcommon(a,b);
247+
}
248+
}
249+
```
250+
189251
## Question 2
190252

191253
## JavaScript Implementation
@@ -282,4 +344,57 @@ function searchCommonElements (arr1, arr2, arr3) {
282344
}
283345

284346
console.log (searchCommonElements ([1, 2, 4, 6, 7, 9, 11, 14, 15], [2, 3, 4, 5, 6, 7, 8, 9], [2, 4, 6, 8])); // [2, 4, 6]
347+
```
348+
349+
## Java Implementation
350+
351+
### [Solution](./Java/Common3.java)
352+
353+
```java
354+
/**
355+
* @date 30/01/19
356+
* @author SPREEHA DUTTA
357+
*/
358+
import java.util.*;
359+
public class Common3 {
360+
public static void findcommon(int a[],int b[],int c[])
361+
{
362+
int i=0,j=0,k=0;
363+
while(true)
364+
{
365+
if(a[i]==b[j] && b[j]==c[k])
366+
System.out.print(a[i]+" ");
367+
if(a[i]<b[j])
368+
i++;
369+
else if(b[j]<c[k])
370+
j++;
371+
else
372+
k++;
373+
if(i==a.length || j==b.length || k==c.length)
374+
break;
375+
}
376+
}
377+
public static void main(String []args)
378+
{
379+
int n,m,k,i;
380+
Scanner sc=new Scanner(System.in);
381+
System.out.println("Enter size of first array ");
382+
n=sc.nextInt();
383+
int a[]=new int[n];
384+
for(i=0;i<n;i++)
385+
a[i]=sc.nextInt();
386+
System.out.println("Enter size of second array ");
387+
m=sc.nextInt();
388+
int b[]=new int[m];
389+
for(i=0;i<m;i++)
390+
b[i]=sc.nextInt();
391+
System.out.println("Enter size of third array ");
392+
k=sc.nextInt();
393+
int c[]=new int[k];
394+
for(i=0;i<k;i++)
395+
c[i]=sc.nextInt();
396+
System.out.println("Common elements are ");
397+
findcommon(a,b,c);
398+
}
399+
}
285400
```

0 commit comments

Comments
 (0)