(Python, Java) 리트코드 - Intersection of Two Arrays

[문제 링크]

Python 풀이

from typing import List


class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        return list(set(nums1) & set(nums2))

Java 풀이

import java.util.*;

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set = new HashSet<>();
        for (Integer num : nums1) {
            set.add(num);
        }

        Set<Integer> answer = new HashSet<>();
        for (int num : nums2) {
            if (set.contains(num))
                answer.add(num);
        }
        return answer.parallelStream().mapToInt(Integer::intValue).toArray();
    }
}

© 2021. By Backtony