Using Set Java


  • 0
    P
    class Solution {
        public int[] intersection(int[] nums1, int[] nums2) {
            
            Set<Integer> mySet = new HashSet<>();
            
            for (int j=0;j<nums1.length;j++){            
                mySet.add(nums1[j]);            
            }
            
            Set<Integer> answer = new HashSet<Integer>();
            for (int i=0;i<nums2.length;i++)
            if (mySet.contains(nums2[i])){            
                answer.add(nums2[i]);            
            }
            System.out.println(answer);
            int[] finala = new int[answer.size()];
            int i=0;
            for (Integer in:answer){
                
                finala[i]=in;
                i++;
                
            }
            return finala;
        }
    }
    

Log in to reply
 

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