-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be14797
commit 9f5c3f6
Showing
3 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* 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 Common3 { | ||
public static void findcommon(int a[],int b[],int c[]) | ||
{ | ||
int i=0,j=0,k=0; | ||
while(true) | ||
{ | ||
if(a[i]==b[j] && b[j]==c[k]) | ||
System.out.print(a[i]+" "); | ||
if(a[i]<b[j]) | ||
i++; | ||
else if(b[j]<c[k]) | ||
j++; | ||
else | ||
k++; | ||
if(i==a.length || j==b.length || k==c.length) | ||
break; | ||
} | ||
} | ||
public static void main(String []args) | ||
{ | ||
int n,m,k,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("Enter size of third array "); | ||
k=sc.nextInt(); | ||
int c[]=new int[k]; | ||
for(i=0;i<k;i++) | ||
c[i]=sc.nextInt(); | ||
System.out.println("Common elements are "); | ||
findcommon(a,b,c); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters