[JAVA] Two HashSet / T : O(N), S : O(N)


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

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.