-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigratory_birds.js
More file actions
35 lines (28 loc) · 832 Bytes
/
migratory_birds.js
File metadata and controls
35 lines (28 loc) · 832 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
const fs = require('fs');
function migratoryBirds(arr) {
const countMap = new Map();
const max = {value: 0, count: 0};
arr.forEach((el) => {
const count = countMap.get(el);
if (count) {
if (count > max.count || (count == max.count && el < max.value)) {
max.value = el;
max.count = count;
}
countMap.set(
el,
count + 1
);
} else {
countMap.set(
el, 1
)
}
});
return max.value;
}
const input01 = [1, 4, 4, 4, 5, 3];
const input02 = [1, 2, 3, 4, 5, 4, 3, 2, 1, 3, 4];
const input04 = fs.readFileSync('input04.txt', 'utf8').split(' ').map(e => parseInt(e, 10));
// console.log(input04);
console.log(migratoryBirds(input04));