import java.util.Arrays;
public class Main {
public static int[] getIntersection(int[] arr1, int[] arr2) {
int[] intersection = new int[Math.min(arr1.length, arr2.length)];
int idx = 0;
for (int i = 0, j = 0; i < arr1.length && j < arr2.length; ) {
if (arr1[i] < arr2[j]) {
i++;
} else if (arr1[i] > arr2[j]) {
j++;
} else {
intersection[idx++] = arr1[i];
i++;
j++;
}
}
return Arrays.copyOf(intersection, idx);
}
public static void main(String[] args) {
int[] numbers1 = {10, 11, 24};
int[] numbers2 = {10, 13, 14, 18, 24, 30};
int[] result1 = Main.getIntersection(numbers1, numbers2);
System.out.println(Arrays.toString(result1)); // => [10, 24]
}
}
int[] numbers1 = {10, 11, 24};
int[] numbers2 = {10, 13, 14, 18, 24, 30};
var result1 = App.getIntersection(numbers1, numbers2);
System.out.println(Arrays.toString(result1)); // => [10, 24]