find lucky integer in javascript code example
Example: find lucky integer in javascript
const findLucky = function(arr) {
let lucky = [-1];
let numObj = {};
for (num of arr) {
numObj[num] ? numObj[num] += 1 : numObj[num] = 1;
}
let nums = Object.keys(numObj);
let frequencies = Object.values(numObj);
for (let num in nums) {
if (nums[num] == frequencies[num]) {
lucky.push(frequencies[num]);
}
}
if (lucky.length === 1) {
return lucky;
} else {
return Math.max(...lucky);
}
};