Intuitive Javascript Solution


  • 0
    D
    var findRestaurant = function(list1, list2) {
        const commonInterest = [];
        
        list1.forEach((res, index1) => {
            const index2 = list2.indexOf(res);
            
            // collect an array of common interests with index sum as index
            if (index2 !== -1) {
                const indexSum = index1 + index2;
                
                if (!commonInterest[indexSum]) commonInterest[indexSum] = [];
                commonInterest[indexSum].push(res);
            }
        });
        
        // return the common interests with the minimum index sum
        for (let i = 0; i < commonInterest.length; i++)
            if (commonInterest[i]) return commonInterest[i];
    };
    

Log in to reply
 

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