### Find the Intersection of Two Arrays in Java

Hi everyone,

In this post, we are going to try to solve an easy programming interview question that is likely to be asked in Java interviews. But before, I suggest you to take a look at 40+ Essential Core Java Interview Questions to refresh your memory on core concepts.

The question is:

Given two arrays, write a function to compute their intersection. The resulting array needs to contain unique integers.

There are a lot of different ways to approach this question, but I prefer to use a Set to store the intersection results. In the following code, first I check the base cases that the input arrays are null or empty. Then I create two sets: one to store the unique elements of the first array and the intersection of the input arrays. As you know, the elements stored in a Set have to be unique, and this is the reason we are using Sets in this example.
``````
package intersection;

import java.util.HashSet;
import java.util.Set;

public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {

if(nums1==null || nums2==null || nums1.length==0 || nums2.length==0) return new int;

Set set = new HashSet<>();
Set intersect = new HashSet<>();

// Iterate through the first array and add the elements to the first set.
for (int i = 0; i < nums1.length; i++) {
}

// Iterate through the second array and whenever there is a match add those numbers to the intersect set.
for (int i = 0; i < nums2.length; i++) {
if (set.contains(nums2[i])) {
}
}
// Create and populate the result array
int[] result = new int[intersect.size()];
int i = 0;
for (Integer num : intersect) {
result[i++] = num;
}
return result;
}
}

``````
After writing the base cases and creating our empty Sets, we iterate through the first array and add the elements to the first set using a for loop. Then we iterate through the second array and whenever there is a match, add those numbers to the intersect set. Finally, we create a new result array of type int, populate this array using the intersect set and return this array.

And these are the test cases to test the functionality of the program:

``````
package intersection;

import static org.junit.Assert.*;

import org.junit.BeforeClass;
import org.junit.Test;

public class TestSolution {
private static Solution s;

@BeforeClass
public static void setUpBeforeClass() throws Exception{
s = new Solution();

}
@Test
public void testIntersection1() {
int[] nums1 = {4,8,1,2,5,12,15,3,8,121};
int[] nums2 = {123,456,678,78,15,99,12};
int[] result =  {12,15};
assertArrayEquals(result,s.intersection(nums1, nums2));
}
// second array is empty
@Test
public void testIntersection2() {
int[] nums1 = {4,8,1,2,5,12,15,3,99,121};
int[] nums2 = {};
int[] result =  {};
assertArrayEquals(result,s.intersection(nums1, nums2));
}
// Empty arrays
@Test
public void testIntersection3() {
int[] nums1 = {};
int[] nums2 = {};
int[] result =  {};
assertArrayEquals(result,s.intersection(nums1, nums2));
}

//first array is empty
@Test
public void testIntersection4() {
int[] nums1 = {};
int[] nums2 = {123,456,678,78,15,99,12};
int[] result =  {};
assertArrayEquals(result,s.intersection(nums1, nums2));
}
//first array is null
@Test
public void testIntersection5() {
int[] nums1 = null;
int[] nums2 = {123,456,678,78,15,99,12};
int[] result =  {};
assertArrayEquals(result,s.intersection(nums1, nums2));
}
//arrays are null
@Test
public void testIntersection6() {
int[] nums1 = null;
int[] nums2 = null;
int[] result =  {};
assertArrayEquals(result,s.intersection(nums1, nums2));
}
}

``````

Here is the test result: 