Simple O(n) solution:
public static Integer[] getIntersection(Integer[] a, Integer[] b) {
Map countsMap = new HashMap();
for(Integer num : a) { // O(n) time
countsMap.merge(num, 1, (x, y) -> y + 1);
}
List intersection = new LinkedList();
for(Integer num : b) { // O(n) time
if(countsMap.containsKey(num)) { // O(1)
int count = countsMap.get(num); // O(1)
if(count > 0) {
intersection.add(num); // O(1)
countsMap.put(num, count - 1); // O(1)
}
}
}
// Above loops run in O(n) time each.
// Total time complexity = O(2n)
return intersection.toArray(new Integer[intersection.size()]);
}