-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmigratoryBirds.js
More file actions
38 lines (28 loc) · 803 Bytes
/
migratoryBirds.js
File metadata and controls
38 lines (28 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* @link Problem definition [[docs/hackerrank/implementation/migratoryBirds.md]]
*/
import util from 'util';
import { logger as console } from '../../logger.js';
function migratoryBirds(arr) {
if (arr.length === 0) {
throw new Error('Empty input');
}
const map = {};
let max = arr[0];
for (const bird of arr) {
console.debug(`bird ${bird}`);
if (!map[bird]) {
map[bird] = 1;
} else {
map[bird] += 1;
}
console.debug(`bird = ${bird} ~> map[bird] = ${map[bird]}`);
console.debug(`max = ${max} ~> map[max] = ${map[max]}`);
if (map[bird] > map[max] || (map[bird] === map[max] && bird < max))
max = bird;
}
console.debug(`map: ${util.inspect(map)}`);
return max;
}
export default { migratoryBirds };
export { migratoryBirds };