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?
=Java Solution 2 – Binary Search
not sure about code
isn’t it just merge sorting?
Here,you have used the function intersection, but how to write it into Java program?
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++){
mySet.add(arr1[i]);
}
for(int i =0 ; i<arr2.length;i++){
mySet1.add(arr2[i]);
}
for(int i =0 ; i< mySet1.size();i++){
if(!mySet.contains(mySet1.get(i))){
mySet.add(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++){
mySet.add(arr1[i]);
}
for(int i=0 ; i<arr2.length;i++){
mySet1.add(arr2[i]);
}
for(int i =0 ; i<mySet1.size();i++){
if(mySet.contains(mySet1.get(i))){
intersection.add(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);
}
}
requirement: the output contains unique values.
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].
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).
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.
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.
Code changed. Thanks!
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