1. /**
    2. * @Description 卑微的简单题,Hash计数法,没啥好说的
    3. * @Date 2022/1/11 11:41 下午
    4. * @Author wuqichuan@zuoyebang.com
    5. **/
    6. public class Solution {
    7. public int[] intersect(int[] nums1, int[] nums2) {
    8. Map<Integer,Integer> map1 = new HashMap<>();
    9. List<Integer> res = new ArrayList<>();
    10. for(int i : nums1){
    11. if(map1.containsKey(i)){
    12. map1.put(i, map1.get(i) + 1);
    13. }else {
    14. map1.put(i, 1);
    15. }
    16. }
    17. for(int i : nums2){
    18. if(map1.containsKey(i)){
    19. if(map1.get(i) > 0 ){
    20. res.add(i);
    21. map1.put(i, map1.get(i) - 1);
    22. }
    23. }
    24. }
    25. int[] resArr = new int[res.size()];
    26. for (int i = 0; i < res.size(); i++) {
    27. resArr[i] = res.get(i);
    28. }
    29. return resArr;
    30. }
    31. }