Answers for "find lucky integer in javascript"

0

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);
    }
};
Posted by: Guest on May-14-2021

Code answers related to "find lucky integer in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language