My O(n) Javascript solution


  • 1
    W
    /**
     * @param {number[]} nums1
     * @param {number[]} nums2
     * @return {number[]}
     */
    var intersection = function(nums1, nums2) {
        var map = {};
        for (var i = 0; i < nums1.length; ++i) {
            if (map[nums1[i]] === undefined)
                map[nums1[i]] = 0;
        }
        for (var i = 0; i < nums2.length; ++i) {
            if (map[nums2[i]] !== undefined)
                map[nums2[i]] = 1;
        }
        var result = [];
        for (var key in map) {
            if (map[key] === 1) {
                result.push(parseInt(key));
            }
        }
        return result;
    };
    

Log in to reply
 

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