My JavaScript Solution


  • 0
    S
    /**
     * @param {number[]} nums1
     * @param {number[]} nums2
     * @return {number[]}
     */
    var intersect = function(nums1, nums2) {
        var hash1 = {},
            hash2 = {},
            res = [];
        for (var i = 0; i < nums1.length; i++) {
            hash1[nums1[i]] = hash1[nums1[i]] ? hash1[nums1[i]] + 1 : 1;
        }
        for (var j = 0; j < nums2.length; j++) {
            hash2[nums2[j]] = hash2[nums2[j]] ? hash2[nums2[j]] + 1 : 1;
            if (hash1[nums2[j]] > 0 && hash2[nums2[j]] <= hash1[nums2[j]]) res.push(nums2[j]);
        }
        return res;
    };
    

Log in to reply
 

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