孙晓聪
  • 最新
  • 博客
  • 书评
  • Intersection of Two Arrays

    Description

    doc

    Solutions

    First Idea

    /**
     * @param {number[]} nums1
     * @param {number[]} nums2
     * @return {number[]}
     */
    var intersection = function (nums1, nums2) {
      const nums2Set = new Set(nums2)
      return [...new Set(nums1)].filter((v) => nums2Set.has(v))
    }
    
    • Time Complexity: O(n)
    • Space Complexity: O(n)