2つの配列の交叉


二つの配列を与え、それらの交差を計算する関数を作成します。
例1:
  : nums1 = [1,2,2,1], nums2 = [2,2]
  : [2]
例2:
  : nums1 = [4,9,5], nums2 = [9,4,9,8,4]
  : [9,4]
説明:
  • 出力結果の各要素は必ず唯一である。
  • 出力結果の順序を考慮しなくてもいいです。
  • class Solution {
        public int[] intersection(int[] nums1, int[] nums2) {
            Map helpMap = new HashMap<>();
            Map resultMap = new HashMap<>();
            for (int temp : nums1) {
                helpMap.put(new Integer(temp), new Integer(temp));
            }
            for (int temp : nums2) {
                if (helpMap.get(new Integer(temp)) != null) {
                    resultMap.put(new Integer(temp), new Integer(temp));
                }
            }
            int[] result = new int[resultMap.size()];
            int i = 0;
            for (Integer temp : resultMap.values()) {
                result[i] = temp;
                ++i;
            }
            return result;
        }
    }
    やはり工具類を使って、自分で実現するのは難しいです。