This function calculates the maximum value in an array, by sorting the array and finding the index of the maximum value. It then creates a new array, with the same length as the original array, but with the maximum value at the first index. Finally, it returns the new array.
function migratoryBirds(arr) {
let lengthOfMaxIndex = arr.length - 1;
let newCalculator = 0;
let newArrOFCal = [];
let newArr = arr.sort((a, b) => a - b);
let finalIndex = 0;
newArr.forEach((item, index, arraySelf) => {
let nextIndex = index + 1;
if (item === newArr[nextIndex]) {
newCalculator++;
} else {
newArrOFCal.push(newCalculator);
newCalculator = 0;
}
});
let increasedCal = newArrOFCal.map((int) => int + 1);
let max = Math.max(...increasedCal);
let impIndex = increasedCal.indexOf(max);
newArrOFCal.forEach((item, index) => {
if (index <= impIndex) {
finalIndex += item;
}
});
return newArr[finalIndex] ;
}