```java
public int[] intersection(int[] nums1,int[] nums2) {
Set<Integer> set1 = new HashSet<>();
for (int value : nums1) {
set1.add(value);
}
Set<Integer> resSet = new HashSet<>();
for (int j=0;j<nums2.length;j++){
if (set1.contains(nums2[j])){
resSet.add(nums2[j]);
}
}
int[] res = new int[resSet.size()];
int k = 0;
for (Integer v : resSet){
res[k] = v;
k++;
}
return res;
```
调用方法的时候传参呀
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
//比较大小,将数组长度小的放前面
if(nums1.length > nums2.length){
return intersect(nums2,nums1);
}
//申明一个Map对象
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
//便利一个第一个长度小的数组nums1,将键值对【数字-对应的个数】存入map中
for(int num : nums1){
int count = map.getOrDefault(num,0)+1;
map.put(num,count);
}
//按短的数组申请一个新数组,最大长度为两个数组中较小的长度
int[] intersection = new int[nums1.length];
//遍历第二个数组,与map中相同的值做对比
int index = 0;
for(int num : nums2){
//count等于num在map的个数
int count = map.getOrDefault(num,0);
if(count > 0){
intersection[index++] = num;
count--;
//重新矫正map中的键值对【数字-对应的个数】
if(count > 0){
map.put(num,count);
}else{
map.remove(num);
}
}
}
//数组intersection[]的最终长度不确定,通过分片截取[0.....index]的数值
return Arrays.copyOfRange(intersection,0,index);
}
}