My JavaScript Solution


  • 0
    L
    /**
     * @param {number[]} nums1
     * @param {number[]} nums2
     * @return {number[]}
     */
    var has_child = function(item, array){
        for (var i=0; i < array.length; i++){
            if (item == array[i]){
                return true;
            }
        }
        return false;
    }
    var intersection = function(nums1, nums2) {
        if (nums2.length > nums1.length){
            var t = nums1;
            nums1 = nums2;
            nums2 = t;
        }
        var intersection_array = [];
        for (var i = 0; i < nums1.length; i++){
            for (var j = 0; j < nums2.length; j++){
                if (nums1[i] == nums2[j]){
                    if (!has_child(nums1[i], intersection_array)){
                        intersection_array.push(nums1[i]);
                    }
                }
            }
        }
        return intersection_array
    };
    

Log in to reply
 

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