JS solution, straight forward.


  • 0
    F
    var intersect = function(nums1, nums2) {
        if(nums1.length === 0 || nums2.length === 0) return [];
        var map1 = {}, map2 = {}, res = [], i, count;
        nums1.forEach(function(item){
            map1[item] = !map1[item] ? 1 : ++map1[item];
        }, this);
        
         nums2.forEach(function(item){
            map2[item] = !map2[item] ? 1 : ++map2[item];
        }, this);
        
        for(var item in map1) {
            if(map2[item]) {
                i = 0;
                count = Math.min(map2[item], map1[item]);
                while(i<count) {
                    i++;
                    res.push(+item);
                }
            }
        }
        return res;
    };
    
    

Log in to reply
 

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