Map Solution


  • 0
    D
        if(nums1.length==0) return nums1;
        if(nums2.length==0) return nums2;
        
     
     List<Integer> ls = new ArrayList<Integer>();
        Map<Integer,Integer> mp = new HashMap<Integer,Integer>();
        
        for(int i=0;i<nums1.length;i++)
        {
            mp.put(nums1[i],mp.getOrDefault(nums1[i],0)+1);
        }
        
        int count=0;
        for(int j=0;j<nums2.length;j++)
        {
            if(mp.containsKey(nums2[j])&& mp.get(nums2[j])>0)
            {
                mp.put(nums2[j],mp.get(nums2[j])-1);
                ls.add(nums2[j]);
              
            }
            
        }
        int [] res = new int[ls.size()];
        for(int k =0;k<ls.size();k++)
        {
            res[k]=ls.get(k);
        }
        return res;

Log in to reply
 

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