# LeetCode – Intersection of Two Arrays (Java)

Given two arrays, write a function to compute their intersection.

Java Solution 1 - HashSet

Time = O(n).
Space = O(n).

```public int[] intersection(int[] nums1, int[] nums2) { HashSet<Integer> set1 = new HashSet<Integer>(); for(int i: nums1){ set1.add(i); }   HashSet<Integer> set2 = new HashSet<Integer>(); for(int i: nums2){ if(set1.contains(i)){ set2.add(i); } }   int[] result = new int[set2.size()]; int i=0; for(int n: set2){ result[i++] = n; }   return result; }```

Java Solution 2 - Binary Search

Time = O(nlog(n)).
Space = O(n).

```public int[] intersection(int[] nums1, int[] nums2) { Arrays.sort(nums1); Arrays.sort(nums2);   ArrayList<Integer> list = new ArrayList<Integer>(); for(int i=0; i<nums1.length; i++){ if(i==0 || (i>0 && nums1[i]!=nums1[i-1])){ if(Arrays.binarySearch(nums2, nums1[i])>-1){ list.add(nums1[i]); } } }   int[] result = new int[list.size()]; int k=0; for(int i: list){ result[k++] = i; }   return result; }```

Any improvement?

Category >> Algorithms
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
```<pre><code>
String foo = "bar";
</code></pre>
```
• alexwest11

=Java Solution 2 – Binary Search

isn’t it just merge sorting?

• Shantanu More

Here,you have used the function intersection, but how to write it into Java program?

• arunmanda

import java.util.ArrayList;
public class union_intersection{
public ArrayList union(int[] arr1, int[] arr2){
ArrayList mySet = new ArrayList();
ArrayList mySet1 = new ArrayList();
for(int i =0 ; i<arr1.length;i++){
}
for(int i =0 ; i<arr2.length;i++){
}
for(int i =0 ; i< mySet1.size();i++){
if(!mySet.contains(mySet1.get(i))){
}

}
return mySet;
}
public ArrayList Intersection(int[] arr1 , int[] arr2){
ArrayList mySet = new ArrayList();
ArrayList mySet1 = new ArrayList();
ArrayList intersection = new ArrayList();
for(int i=0 ; i<arr1.length;i++){
}
for(int i=0 ; i<arr2.length;i++){
}
for(int i =0 ; i<mySet1.size();i++){
if(mySet.contains(mySet1.get(i))){

}
}
return intersection;
}
public static void main(String[] args) {
union_intersection union = new union_intersection();
int[] arr1 = {1,2,3,4};
int[] arr2 = {2,3,4,5};
ArrayList final_array_union = union.union(arr1, arr2);
System.out.println(final_array_union);
ArrayList final_array_intersection = union.Intersection(arr1, arr2);
System.out.println(final_array_intersection);

}

}

• Cong Wang

requirement: the output contains unique values.

• rokyslash

Both the programs are wrong. They dont work for inputs [1,2,2,1], [2,2]. The expected output in this case would be [2,2] but your programs only output [2].

• Zevgon

I think there might be another possible improvement. Not positive, but here’s what I’m thinking:

Sort the shorter array, then iterate through the longer array, doing a modified bsearch on the sorted array for each element. Keep track of the indices in the sorted array that have already been used, and pass that set into the modified bsearch as invalid indices.

I think that would be time complexity of O(mlog(n)), a little better than O(nlog(n) + mlog(m)). I think potentially worse space complexity of 2n because of keeping track of the invalid indices, but still O(n).

• strmrdr

I believe the time complexity for the binary search method is a much more than O(nlog(n)) considering that it takes that time just to sort a single array- you are sorting two arrays and then performing a binary search on every element of one of the arrays.

• Praveen Singh

try to get the intersection between below arrays using binary search –
int[] arr1 = {7, 18, 24, 32, 44, 57, 40, 22, 8, 38};
int[] arr2 = {8, 0, 29, 9, 18};

I tried, But 18 is not coming in intersection.

• ryanlr

Code changed. Thanks!

• Anusha

Yes, what you can do in set two is, without adding all the elements, add the ones that are present in the array1. Less space, and you don’t have to call iter.remove(); a slight performance optimization