DEV Community

Swapnil Gupta
Swapnil Gupta

Posted on

Intersection of Two Arrays - II

Leet Code Problem Link: https://leetcode.com/problems/intersection-of-two-arrays-ii/
follow: twitter | LinkedIn

Method 1: Two-Pointer Approach with image explanation(https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/954800/Java-solution-with-explanation-and-pictures)

Method 2: Using Frequency HashMap

learn about HashMaps and Some more problems

creating Frequency HashMaps : Frequency Hashmaps are created using :

 if (charCountMap.containsKey(c)) {

                // If char is present in charCountMap,
                // incrementing it's count by 1
                charCountMap.put(c, charCountMap.get(c) + 1);
            }
            else {

                // If char is not present in charCountMap,
                // putting this char to charCountMap with 1 as it's value
                charCountMap.put(c, 1);
            }
        }
Enter fullscreen mode Exit fullscreen mode

The solution can be given as such, where will create two hashmaps and we will put frequency of occurrence in that and compare :

class Solution (
  public int[] intersect(int[] nums1, int[] nums2) {
        List<Intger> arr = new ArrayList<Integer>();
        HashMap<integer,Integer> mapl =new HashMap<> ();
        HashMap<Integer,Integer> map2 = new HashMapc>();
        for(int i =0; i < nums1.length; i++)(
            if(map1.containskey(nums1[i]))
              map1.put(nums1[i), maps.get (nums1[i]) +1);
            else
              maps1.put (nums1[i], 1);
       for (int i=0; i<nums2.length; i++){
            if(maps2.containskey(nums2[i]))
             maps2.put (nums2[i], maps.get(nums2[i]) +1);
       else
               maps2.put (nums2[i], 1);
//if key of hashmap2 is present in hashmap1 then  
//find the minimum frequency of character between these has maps
        for(Integer key: map1.keyset ())(
            if(map2.containskey(key)){
                int x= Math.min(map2.get(key), mapl.get (key));
                while(x-- > 0){
                    arr.add(key);
//converting list to array
        int result[] - new int [arr.size()];
        for(int i 0; i carr.size(); i++)
            result[1] - arr.get(1);
        return result;

Enter fullscreen mode Exit fullscreen mode

Method 3: Using one HashMap: solution

The one HashMap method can also be used instead of two and we will use the frequency property of HashMap.

The only point that we have to remember is to decrease the count of the frequency in nums2[] hashmap, as we want to register the frequency only that number of time as in the nums1[].

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        HashMap<Integer,Integer> map= new HashMap<Integer,Integer>();
        ArrayList<Integer> ans= new ArrayList<Integer>();
        for ( int i = 0 ; i < nums1.length; i++){
            //frequency counter: charCountMap.put(c, charCountMap.get(c) + 1);
            if(map.containsKey(nums1[i]))
                map.put(nums1[i],map.get(nums1[i])+1);
                else map.put(nums1[i],1);
                }  
        for (int i = 0 ; i <nums2.length; i++){
        if(map.containsKey(nums2[i]) && map.get(nums2[i]) > 0){
            map.put(nums2[i],map.get(nums2[i])-1);
            ans.add(nums2[i]);
            } 
          }

    //  Now we just turn our list into an array using get method and return.
    int[] result = new int[ans.size()];
    for (int i = 0 ; i <ans.size(); i++){
        result[i] = ans.get(i);
    }

    return result;

            }
        }
Enter fullscreen mode Exit fullscreen mode

Reference material:
Different ways of converting Lists to Arrays: .get method, list.toArray(myArray)

Top comments (0)