森田賢二のleetCodeの使い方、解説、解答を毎日更新しているキャラクター

[leetCode] 349. Intersection of Two Arrays むー

[leetCode] 349. Intersection of Two Arrays むー

ループ処理がうまくいっていない模様

var intersection = function(nums1, nums2) {
    let arr = [];
    nums1.sort()
    nums2.sort()
    if(nums1.length > nums2.length){
        [nums1, nums2] = [nums2, nums1]
    }
    for(let i = 1; i < nums1.length; i++){
        while(nums1[i] === nums1[i-1]) i++
        helper(nums2, nums1[i], i, nums2.length)
    }
    function helper(nums, target, left, right){
        let l = left;
        let r = right
        while(l < r){
            let mid = Math.floor(l + (r - l) / 2);
            if(nums[mid] === target){
                arr.push(target)
                return
            } else if(nums[mid] < target){
                l = mid + 1
            } else if(nums[mid] > target){
                r = mid -1
            }

        }
        return
    }

    return arr
};