Skip to content

Latest commit

 

History

History
28 lines (25 loc) · 682 Bytes

349. 两个数组的交集.md

File metadata and controls

28 lines (25 loc) · 682 Bytes

Meta


class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set = new HashSet<>();
        for (int num : nums1) {
            set.add(num);
        }
        Set<Integer> result = new HashSet<>();
        for (int num : nums2) {
            if (set.contains(num)) {
                result.add(num);
            }
        }
        // Integer List or Set to int array
        return result.stream().mapToInt(Integer::intValue).toArray();
    }
}