-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path349.intersection-of-two-arrays.js
66 lines (62 loc) · 1.47 KB
/
349.intersection-of-two-arrays.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* @lc app=leetcode id=349 lang=javascript
*
* [349] Intersection of Two Arrays
*/
// @lc code=start
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number[]}
*/
// var intersection = function(nums1, nums2) {
// if (nums1.length === 0 || nums2.length === 0) {
// return []
// }
// let map = Object.create(null)
// let result = []
// for (let i = 0; i < nums1.length; i++) {
// if (map[nums1[i]] === undefined) map[nums1[i]] = 1
// for (let j = 0; j < nums2.length; j++) {
// if (map[nums2[j]] === 1) {
// map[nums2[j]] = 2
// result.push(nums2[j])
// }
// }
// }
// return result
// }
//12ms
// var intersection = function(nums1, nums2) {
// if (nums1.length === 0 || nums2.length === 0) {
// return []
// }
// let set = new Set()
// let intersection = new Set()
// for (let i = 0; i < nums1.length; i++) {
// set.add(nums1[i])
// }
// for (let i = 0; i < nums2.length; i++) {
// if (set.has(nums2[i])) intersection.add(nums2[i])
// }
// return Array.from(intersection)
// }
// // 7ms
// var intersection = function(nums1, nums2) {
// const set = new Set(nums1)
// return [...new Set(nums2.filter(n => set.has(n)))]
// }
// 5ms
var intersection = function(nums1, nums2) {
nums1 = new Set(nums1)
nums2 = new Set(nums2)
const common = []
nums2.forEach(num => {
if (nums1.has(num)) common.push(num)
})
return common
}
// @lc code=end
module.exports = {
intersection
}