Intersection of two sorted arrays code in java
import java.util.*;
public class MyFirstClass {
public static void main(String[] args) {
int arr1[] = {1,1,2,3,4,4,5};
int arr2[] = {2,3,4,4,5,10,20,100};
int n1 = arr1.length; int n2=arr2.length;
int leng = n1+n2;
int i=0;int j=0;
ArrayList<Integer> Intersection = new ArrayList<>();
while(i<n1 && j<n2)
{
if(arr1[i]<arr2[j])
{
i++;
}
else if(arr2[j] < arr1[i])
{
j++;
}
else
{
Intersection.add(arr1[i]);
i++;
j++;
}
}
System.out.println(Intersection);
}
}
No comments:
Post a Comment