Skip to content

Commit de52b14

Browse files
authored
Create 1394-find-lucky-integer-in-an-array.js
1 parent 9845a38 commit de52b14

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} arr
3+
* @return {number}
4+
*/
5+
const findLucky = function(arr) {
6+
const hash = {}
7+
for(let e of arr) hash[e] = (hash[e] || 0) + 1
8+
let res
9+
Object.keys(hash).forEach(k => {
10+
if(+k === hash[k]) {
11+
if (res == null) res = hash[k]
12+
else {
13+
if (hash[k] > res) res = hash[k]
14+
}
15+
}
16+
})
17+
return res == null ? -1 : res
18+
};

0 commit comments

Comments
 (0)