payal-kothari
7/25/2017 - 5:40 PM

Complexity - O(n) From https://leetcode.com/problems/intersection-of-two-arrays/#/description

public class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        
        List<Integer> list = new ArrayList<>();
        Set<Integer> intersect = new HashSet<>();
        
        for(int num : nums1){
            list.add(num);
        }
        
        for(int num : nums2 ){
            if(list.contains(num)){
                intersect.add(num);
            }
        }
        
        int[] ans = new int[intersect.size()];        // ***
        int i =0;
        for(Integer num : intersect){                 // *** convert set to int[]
            ans[i] = num;
            i++;
        }
        
        return ans;
        
    }
}