Also Read: Java Program to Find Union of two Arrays

Java Program to Find Intersection of two Arrays

import java.util.Scanner; //import Scanner class in our program


class demo
{
 public static void main(String…s)
 {
 int i,j,n1,n2;
 Scanner sc=new Scanner(System.in); //used to read from keyboard


 System.out.print(“Enter number of elements of first array:”);
 n1=sc.nextInt();
 System.out.print(“Enter number of elements of second array:”);
 n2=sc.nextInt();


 int a1[]=new int[n1];
 int a2[]=new int[n2];


 System.out.print(“nEnter elements of first array in ascending order:”);
 for(i=0;i<n1;++i)
 a1[i]=sc.nextInt();


 System.out.print(“nEnter elements of second array in ascending order:”);
 for(i=0;i<n2;++i)
 a2[i]=sc.nextInt();




 i=j=0;
 System.out.print(“nIntersection of Arrays: “);
 while(i<n1&&j<n2)
 {
 if(a1[i]<a2[j])
 i++;
 else
 if(a2[j]<a1[i])
 j++;
 else
 {
 System.out.print(a1[i]+” “);
 i++;
 j++;
 }
 }
 
 }
}





Also Read: Java Program to Find Union of two Arrays

Java Program to Find Intersection of two Arrays

import java.util.Scanner; //import Scanner class in our program


class demo 
{ 
 public static void main(String…s) 
 { 
 int i,j,n1,n2; 
 Scanner sc=new Scanner(System.in); //used to read from keyboard 

 
 System.out.print(“Enter number of elements of first array:”); 
 n1=sc.nextInt(); 
 System.out.print(“Enter number of elements of second array:”); 
 n2=sc.nextInt(); 

 
 int a1[]=new int[n1]; 
 int a2[]=new int[n2]; 

 
 System.out.print(“nEnter elements of first array in ascending order:”); 
 for(i=0;i<n1;++i) 
 a1[i]=sc.nextInt(); 

 
 System.out.print(“nEnter elements of second array in ascending order:”); 
 for(i=0;i<n2;++i) 
 a2[i]=sc.nextInt(); 

 

 
 i=j=0; 
 System.out.print(“nIntersection of Arrays: “); 
 while(i<n1&&j<n2) 
 { 
 if(a1[i]<a2[j]) 
 i++; 
 else 
 if(a2[j]<a1[i]) 
 j++; 
 else 
 { 
 System.out.print(a1[i]+” “); 
 i++; 
 j++; 
 } 
 } 
 
 } 
}





翻译自: https://www.thecrazyprogrammer.com/2014/07/java-program-to-find-intersection-of-two-arrays.html