Java8 3 lines O(n)


  • 0
    R
    class Solution {
        public int[] intersection(int[] nums1, int[] nums2) {
           Set<Integer> s1 = new HashSet<Integer>(Arrays.asList(IntStream.of(nums1).boxed().toArray(Integer[]::new)));
           s1.retainAll     (new HashSet<Integer>(Arrays.asList(IntStream.of(nums2).boxed().toArray(Integer[]::new))));
           return Stream.of(s1.toArray(new Integer[s1.size()])).mapToInt(Number::intValue).toArray();
        }
    }
    

Log in to reply
 

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