|
| 1 | +# 349. Intersection of Two Arrays - LeetCode Solution |
| 2 | +LeetCode problem link: [349. Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays), |
| 3 | +[349. 两个数组的交集](https://leetcode.cn/problems/intersection-of-two-arrays) |
| 4 | + |
| 5 | +[中文题解](#中文题解) |
| 6 | + |
| 7 | +## LeetCode problem description |
| 8 | +Given two integer arrays `nums1` and `nums2`, return _an array of their **intersection**_. |
| 9 | +Each element in the result must be **unique** and you may return the result in **any order**. |
| 10 | + |
| 11 | +Difficulty: **Easy** |
| 12 | + |
| 13 | +### [Example 1] |
| 14 | +**Input**: `nums1 = [1,2,2,1], nums2 = [2,2]` |
| 15 | + |
| 16 | +**Output**: `[2]` |
| 17 | + |
| 18 | +### [Example 2] |
| 19 | +**Input**: `nums1 = [4,9,5], nums2 = [9,4,9,8,4]` |
| 20 | + |
| 21 | +**Output**: `[9,4]` or `[4,9]` |
| 22 | + |
| 23 | +### [Constraints] |
| 24 | +- `1 <= nums1.length, nums2.length <= 1000` |
| 25 | +- `0 <= nums1[i], nums2[i] <= 1000` |
| 26 | + |
| 27 | +## Intuition behind the Solution |
| 28 | +[中文题解](#中文题解) |
| 29 | + |
| 30 | +1. Convert one of the arrays to a `set`. The elements are unique in a `set`. |
| 31 | +2. When traversing the other array, if the an element is found to already exist in the `set`, it means that the element belongs to the intersection, and the element should be added to the `results`. |
| 32 | +3. The `results` is also of `set` type because duplicate removal is required. |
| 33 | + |
| 34 | +## Complexity |
| 35 | +* Time: `O(n)`. |
| 36 | +* Space: `O(n)`. |
| 37 | + |
| 38 | +## Java |
| 39 | +```java |
| 40 | +class Solution { |
| 41 | + public int[] intersection(int[] nums1, int[] nums2) { |
| 42 | + var results = new HashSet<Integer>(); |
| 43 | + var num1Set = new HashSet<Integer>(); |
| 44 | + |
| 45 | + for (var num : nums1) { |
| 46 | + num1Set.add(num); |
| 47 | + } |
| 48 | + |
| 49 | + for (var num : nums2) { |
| 50 | + if (num1Set.contains(num)) { |
| 51 | + results.add(num); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return results.stream().mapToInt(num -> num).toArray(); |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +## Python |
| 61 | +```python |
| 62 | +class Solution: |
| 63 | + def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: |
| 64 | + set_of_nums1 = set(nums1) |
| 65 | + results = set() |
| 66 | + |
| 67 | + for num in nums2: |
| 68 | + if num in set_of_nums1: |
| 69 | + results.add(num) |
| 70 | + |
| 71 | + return list(results) |
| 72 | +``` |
| 73 | + |
| 74 | +## C++ |
| 75 | +```cpp |
| 76 | +class Solution { |
| 77 | +public: |
| 78 | + vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { |
| 79 | + unordered_set<int> results; |
| 80 | + unordered_set<int> set_of_nums1(nums1.begin(), nums1.end()); |
| 81 | + |
| 82 | + for (auto num : nums2) { |
| 83 | + if (set_of_nums1.contains(num)) { |
| 84 | + results.insert(num); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return vector<int>(results.begin(), results.end()); |
| 89 | + } |
| 90 | +}; |
| 91 | +``` |
| 92 | + |
| 93 | +## JavaScript |
| 94 | +```javascript |
| 95 | +var intersection = function (nums1, nums2) { |
| 96 | + let results = new Set() |
| 97 | + let num1Set = new Set(nums1) |
| 98 | + |
| 99 | + for (let num of nums2) { |
| 100 | + if (num1Set.has(num)) { |
| 101 | + results.add(num) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return Array.from(results) |
| 106 | +}; |
| 107 | +``` |
| 108 | + |
| 109 | +## C# |
| 110 | +```c# |
| 111 | +public class Solution |
| 112 | +{ |
| 113 | + public int[] Intersection(int[] nums1, int[] nums2) |
| 114 | + { |
| 115 | + var results = new HashSet<int>(); |
| 116 | + var num1Set = new HashSet<int>(); |
| 117 | + |
| 118 | + foreach (int num in nums1) |
| 119 | + num1Set.Add(num); |
| 120 | + |
| 121 | + foreach (int num in nums2) |
| 122 | + { |
| 123 | + if (num1Set.Contains(num)) |
| 124 | + { |
| 125 | + results.Add(num); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return results.ToArray(); |
| 130 | + } |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +## Go |
| 135 | +```go |
| 136 | +func intersection(nums1 []int, nums2 []int) []int { |
| 137 | + results := map[int]bool{} |
| 138 | + num1Set := map[int]bool{} |
| 139 | + |
| 140 | + for _, num := range nums1 { |
| 141 | + num1Set[num] = true |
| 142 | + } |
| 143 | + |
| 144 | + for _, num := range nums2 { |
| 145 | + if _, ok := num1Set[num]; ok { |
| 146 | + results[num] = true |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return slices.Collect(maps.Keys(results)) |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +## Ruby |
| 155 | +```ruby |
| 156 | +def intersection(nums1, nums2) |
| 157 | + set_of_nums1 = Set.new(nums1) |
| 158 | + results = Set.new |
| 159 | + |
| 160 | + nums2.each do |num| |
| 161 | + if set_of_nums1.include?(num) |
| 162 | + results << num |
| 163 | + end |
| 164 | + end |
| 165 | + |
| 166 | + results.to_a |
| 167 | +end |
| 168 | +``` |
| 169 | + |
| 170 | +## C |
| 171 | +```c |
| 172 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 173 | +``` |
| 174 | + |
| 175 | +## Kotlin |
| 176 | +```kotlin |
| 177 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 178 | +``` |
| 179 | + |
| 180 | +## Swift |
| 181 | +```swift |
| 182 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 183 | +``` |
| 184 | + |
| 185 | +## Rust |
| 186 | +```rust |
| 187 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 188 | +``` |
| 189 | + |
| 190 | +## Other languages |
| 191 | +``` |
| 192 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 193 | +``` |
| 194 | + |
| 195 | +## 问题描述 |
| 196 | +给定两个数组 `nums1` 和 `nums2` ,返回 _它们的 **交集**_ 。输出结果中的每个元素一定是 **唯一** 的。我们可以 **不考虑输出结果的顺序** 。 |
| 197 | + |
| 198 | +难度: **容易** |
| 199 | + |
| 200 | +### [示例 1] |
| 201 | +**输入**: `nums1 = [1,2,2,1], nums2 = [2,2]` |
| 202 | + |
| 203 | +**输出**: `[2]` |
| 204 | + |
| 205 | +### [示例 2] |
| 206 | +**输入**: `nums1 = [4,9,5], nums2 = [9,4,9,8,4]` |
| 207 | + |
| 208 | +**输出**: `[9,4]` 或者 `[4,9]` |
| 209 | + |
| 210 | +# 中文题解 |
| 211 | +## 思路 |
| 212 | +1. 把其中一个数组转为`set`,数据结构`set`的特点是元素不重复。 |
| 213 | +2. 遍历另一个数组时,如果发现当前元素已经存在于`set`中,则说明该元素属于交集,将该元素加入结果集中。 |
| 214 | +3. 结果集也采用`set`类型,因为需要去重。 |
0 commit comments