java o(n) solution


  • 0
    R

    public int[] intersection(int[] nums1, int[] nums2) {
    Set<Integer> result = new HashSet<>();

        List<Integer> numList = Arrays.stream(nums2).boxed().collect(Collectors.toList());
    
        for (int a1 : nums1) {
            if (numList.contains(a1)) {
                result.add(a1);
            }
        }
    
    
        int[] res = new int[result.size()];
    
        int i = 0;
        for (Integer aResult : result) {
            res[i] = aResult;
            i++;
        }
    
        return res;
    }

Log in to reply
 

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