/*
 * @lc app=leetcode.cn id=349 lang=javascript
 *
 * [349] 两个数组的交集
 */

// @lc code=start
/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number[]}
 */
var intersection = function (nums1, nums2) {
  const n1 = nums1.length, n2 = nums2.length;
  const ans = []

  const hash = new Map()
  for (let i = 0; i < n1; i++) {
    if (!hash.has(nums1[i])) {
      hash.set(nums1[i], nums1[i])
    }
  }

  for (let j = 0; j < n2; j++) {
    if (hash.has(nums2[j]) && !ans.includes(nums2[j])) {
      ans.push(hash.get(nums2[j]))
    }
  }

  return ans;
};
// @lc code=end