Skip to content

Instantly share code, notes, and snippets.

@koji
Created July 31, 2020 05:16
Show Gist options
  • Save koji/f321c3e9191f64f1d3ac7ed93e177328 to your computer and use it in GitHub Desktop.
Save koji/f321c3e9191f64f1d3ac7ed93e177328 to your computer and use it in GitHub Desktop.
js hashmap
var intersect = function(nums1, nums2) {
    const result = [];
    hashMap = {};
        
    for(let i=0; i<nums1.length; i++) {
        // console.log(num);
        if(hashMap[nums1[i]]) {
            hashMap[nums1[i]] +=1;
        } else {
            hashMap[nums1[i]] = 1;
        }
        // hashMap[num] = hashMap[num] ? hashMap[num] + 1 : 1;
    }
    
    for(let i=0; i<nums2.length; i++) {
        if(hashMap[nums2[i]] > 0) {
            // console.log(num);
            result.push(nums2[i]);
            hashMap[nums2[i]] -=1;
        }
    }
    // console.log(result)
    return result;
    
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment