Fast/Simple solution by Java 8 Stream API


  • 0
    X
    public int[] intersection(int[] nums1, int[] nums2) {
        final Set<Integer> set = IntStream.of(nums1).collect(HashSet::new, (s, e) -> s.add(e), Set::addAll);
        return IntStream.of(nums2).filter(i -> set.contains(i)).distinct().toArray();
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.