/**
* @Description 卑微的简单题,Hash计数法,没啥好说的
* @Date 2022/1/11 11:41 下午
* @Author wuqichuan@zuoyebang.com
**/
public class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
Map<Integer,Integer> map1 = new HashMap<>();
List<Integer> res = new ArrayList<>();
for(int i : nums1){
if(map1.containsKey(i)){
map1.put(i, map1.get(i) + 1);
}else {
map1.put(i, 1);
}
}
for(int i : nums2){
if(map1.containsKey(i)){
if(map1.get(i) > 0 ){
res.add(i);
map1.put(i, map1.get(i) - 1);
}
}
}
int[] resArr = new int[res.size()];
for (int i = 0; i < res.size(); i++) {
resArr[i] = res.get(i);
}
return resArr;
}
}