Javascript Solution


  • 0
    var intersection = function(nums1, nums2) {
        var hash = nums1.reduce(function(p, c){
        	p[c] = p[c] ? ++p[c] : 1; 
        	return p;
        }, {});
    
        var hash2 = {};
        return nums2.reduce(function(p, c) { 
        	if ( hash[c] && !hash2[c]) {
        		p.push(c);
        		hash2[c] = hash2[c] ? ++hash2[c] : 1; 
        	}
        	return p;
        }, []);
    };

Log in to reply
 

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