showing results for - "migratory bird problem in javascript"
Estelle
21 Feb 2020
1function MigratoryBird(arr){
2  let map = {}, maxCount = 0, output = [];
3    for (let i = 0; i < arr.length; i++) {
4        if (!map[arr[i]]) {
5            map[arr[i]] = 1;
6        } else {
7            map[arr[i]] += 1;
8        }
9
10        // getting max count
11        if (map[arr[i]] > maxCount) {
12            maxCount = map[arr[i]];
13        }
14    }
15
16
17    for (let j = 0; j < arr.length; j++) {
18        if (map[arr[j]] === maxCount) {
19            output.push(arr[j]);
20        }
21    }
22
23    return Math.min(...output);
24}