# Why this JavaScript code submitted with error "Memory Limit Exceeded"

• ``````/**
* @param {number[][]} points
* @return {number}
*/
var numberOfBoomerangs = function(points) {
var count = 0;
for(let i = 0; i < points.length; i++){
var map = {};
for(let j = 0; j < points.length; j++){
if(j === i) continue;
var dis = getDistance(points[i], points[j]);
if(map[dis] === undefined) map[dis] = 1;
else map[dis]++;
}
for(var x in map){
if(map[x] > 1) count += getCom(map[x]);
}
}
return count;
};

var getDistance = (x, y) => { return Math.pow(x[0] - y[0], 2) + Math.pow(x[1] - y[1], 2); };

var getCom = (x) => { return x * (x - 1) };
``````

• This post is deleted!

• I'm running into the same issue with the following code. It says that 31/31 tests pass but there is a memory limit error.

``````var numberOfBoomerangs = function(points) {
let total = 0;
let store = {};

for(let i = 0; i < points.length; i++) {
for(let j = 0; j < points.length; j++) {
if(i !== j) {
const dx = points[j][0] - points[i][0];
const dy = points[j][1] - points[i][1];
const distance = dx*dx + dy*dy;

if(store[distance] !== undefined) {
store[distance]++;
} else {
store[distance] = 1;
}
}
}

for(let key in store) {
if(store.hasOwnProperty(key)) {
const val = store[key];
total += val * (val - 1);
}
}

store = {};
}

};
``````

• I did something very similar and I don't run into the memory limit issue (oh, tried oztheozoz's and it is working now):

``````var numberOfBoomerangs = function(points) {
var booms = 0;
var distances = {};
for(var i in points){
distances = {};
for(var j = 0; j < points.length; j++){
var d = distanceBetween(points[i], points[j]);
if(!distances[d]){
distances[d] = 1;
}else{
distances[d]++;
}
}
for(var d in distances)
if(distances[d] > 1){
booms += distances[d] * (distances[d] - 1);
}
}

return booms;
};
function distanceBetween(point1, point2){
return (point1[0] - point2[0]) * (point1[0] - point2[0]) + ( point1[1] - point2[1]) * (point1[1] - point2[1]) ;
}
``````

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