The following is more applicable assuming the exclusion of duplicates, which I note since as @SimonAndreForsberg says it isn't obvious, and none of the input tests used even include duplicates, so there's no way to know for sure.

Java Collection have a retainAll method. I hadn't used it before, but noticing its application to this problem led to:


Altering toIntArray to convert a string array toIntSet implementing LinkedHashSet to preserve order
Simply calling retainAll when finding intersection. 


The result: 

private static String findIntersection(Set&lt;Integer&gt; a, Set&lt;Integer&gt; b) {
        StringBuilder result = new StringBuilder();
        a.retainAll(b);

        for (int n : a) {
            result.append(',').append(n);
        }

        return result.length() == 0 ? "" : result.toString().substring(1);
    }

private static Set&lt;Integer&gt; toIntSet(String[] array) {
    Set&lt;Integer&gt; nums = new LinkedHashSet&lt;&gt;();

    for (int i = 0; i &lt; array.length; i++) {
        nums.add(Integer.parseInt(array[i]));
    }

    return nums;
}


This suffers no edge-case bugs unlike the originally posted code, but what I realized is retainAll is painfully inefficient.

So, I began to redesign it accounting for the fact that numbers are sorted and I'm preserving such, but then @janos posted his answer, his answer coupled with the fact that none of the input cases contained negative integers led to this: 

private static String findIntersection(String[] arr1, String[] arr2) {
    StringBuilder result = new StringBuilder();
    int val1 = Integer.parseInt(arr1[0]);
    int val2 = Integer.parseInt(arr2[0]);

    for (int i = 0, j = 0; val1 != -1 &amp;&amp; val2 != -1; ) {

        if (val1 == val2) {
            result.append(',').append(val1);
            val1 = ++i &lt; arr1.length ? Integer.parseInt(arr1[i]) : -1;
            val2 = ++j &lt; arr2.length ? Integer.parseInt(arr2[j]) : -1;
        } else if (val1 &lt; val2) {
            val1 = ++i &lt; arr1.length ? Integer.parseInt(arr1[i]) : -1;
        } else {
            val2 = ++j &lt; arr2.length ? Integer.parseInt(arr2[j]) : -1;
        }
    }

    return result.length() == 0 ? "" : result.substring(1);
}


Which foregoes the disadvantage of his suggestion. It's a unique case that allows this to work, but is applicable elsewhere for Objects and using null

Two sets came to an intersection
I have two 2D array, x(ni, nj) and y(ni,nj), that I need to interpolate over one axis. I want to interpolate along last axis for every ni.

I wrote

import numpy as np
from scipy.interpolate import interp1d

z = np.asarray([200,300,400,500,600])
out = []
for i in range(ni):
    f = interp1d(x[i,:], y[i,:], kind='linear')
    out.append(f(z))
out = np.asarray(out)


However, I think this method is inefficient and slow due to loop if array size is too large. What is the fastest way to interpolate multi-dimensional array like this? Is there any way to perform linear and cubic interpolation without loop? Thanks.
