This is post to find Lucky Integer in an Array Javascript Leetcode solution. For problem statement click here
Javascript program to find lucky integer in an array
var findLucky = function(arr) {
let max = -1, hash = {};
for (let a of arr) {
if (!hash[a]) {
hash[a] = 1;
} else {
hash[a]++;
}
}
for (let key in hash) {
if (key == hash[key]) {
max = Math.max(key, max);
}
}
return max;
};
Time Complexity : O(n)
Space Complexity : O(n)