codingninjas code example
Example: codingninja
import java.util.*;
public class Solution{
public static void intersection(int[] arr1, int[] arr2){
Arrays.sort(arr1);
Arrays.sort(arr2);
int n = arr1.length;
int m = arr2.length;
int i = 0; // pointer starts at arr1
int j = 0; // pointer starts at arr2;
while(i < n && j < m){
if(arr1[i] == arr2[j]) {
System.out.print(arr1[i] + " ");
i++;
j++;
} else if(arr1[i] < arr2[j])
i++;
else
j++;
}
}
}