Created
May 20, 2025 19:10
-
-
Save jac18281828/2180b5aa63365cfd727f02d9d41bb723 to your computer and use it in GitHub Desktop.
Migratory Birds Hackerrank
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function migratoryBirds(arr: number[]): number { | |
// Write your code here | |
let birdCount: { [key: number]: number} = {} | |
let maxId = 0 | |
let maxIdCount = 0 | |
arr.forEach(bid => { | |
if (birdCount[bid] !== undefined) { | |
birdCount[bid]++ | |
} else { | |
birdCount[bid] = 1 | |
} | |
if (birdCount[bid] > maxIdCount) { | |
maxId = bid | |
maxIdCount = birdCount[bid] | |
} else if (birdCount[bid] === maxIdCount && bid < maxId) { | |
maxId = bid | |
} | |
}) | |
return maxId | |
} | |
const birds = [ | |
1, 5, 5, 5, 4, 4, 4, 3 | |
] | |
const birdid = migratoryBirds(birds) | |
console.log(birdid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment