numberOfBoomerangs with JavaScript


  • 0
    N
    /**
     * @param {number[][]} points
     * @return {number}
     */
    var numberOfBoomerangs = function(points) {
        let numOfBoomerangs = 0;
    
        const distance = (p1, p2) => {
            return (p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2  ;
        };
    
        for(let i = 0; i < points.length; i += 1) {
            let boomerangs = {};
    
            for(let j = 0; j < points.length; j += 1) {
                let dist = distance(points[i], points[j]);
                boomerangs[dist] = boomerangs[dist] + 1 || 1;
            }
    
            for(let num in boomerangs) {
                numOfBoomerangs += boomerangs[num] * (boomerangs[num] - 1);
            }
        }
    
        return numOfBoomerangs;
    };
    

Log in to reply
 

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