In between looking at React I have been taking a look at some of the coding tasks on HackerRank mostly to help get a handle of a lot of the new functional Javascript functionality I have not used since classic ASP days. So here I make use of the sort method and the new Map object. The Map object in particular had difficulty in identifying ‘undefined’. In the end I found the ‘has’ method and came up with the below solution.
function migratoryBirds(arr) {
let bMap = new Map();
let sarr = arr.sort(function (a, b) { return a - b });
var curval = 1;
for (var i = 0; i < sarr.length; i++) {
if (bMap.has(sarr[i])) {
curval = bMap.get(sarr[i]);
bMap.set(sarr[i], curval + 1);
}
else {
bMap.set(sarr[i], 1);
}
}
let maxval = bMap.get(sarr[0]);
let minkey = sarr[0];
bMap.forEach(function (value, key) {
if (maxval < value) {
maxval = value;
minkey = key;
}
})
/*return Array.from(bMap);*/
return minkey
}