javascript solution Intersection of Two Arrays II 97%


  • 0
    A
    /**
     * @param {number[]} nums1
     * @param {number[]} nums2
     * @return {number[]}
     */
    var intersect = function(nums1, nums2) {
        var nums1Len = nums1.length,
        	nums2Len = nums2.length,
        	i = 0,
        	indexToDel = -1,
        	resArray = [];
    
    
        if (nums1Len > nums2Len) {
        	for (; i < nums2.length; i++){
    			indexToDel = nums1.indexOf(nums2[i]);
        		if (indexToDel> -1) {
        			resArray.push(nums1[indexToDel]);
        			nums1.splice(indexToDel, 1);
        		}	
        	}
    	} else {
    		for (; i < nums1.length; i++){
    			indexToDel = nums2.indexOf(nums1[i]);
        		if (indexToDel > -1) {
        			resArray.push(nums2[indexToDel]);
        			nums2.splice(indexToDel, 1);
        		}	
        	}
    	}
    
    	return resArray;
    };
    

Log in to reply
 

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