Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2]
.
Note:
- Each element in the result must be unique.
- The result can be in any order.
根据题意,要求两个集合的交集。
思路:因为java中的set集合不包含重复元素,可以利用set集合来完成。
具体实现的java代码如下:
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
int len1 = nums1.length;
int len2 = nums2.length;
Set
set = new HashSet
();
Set
interSet = new HashSet
();
for (int i=0; i