Skip to content

Instantly share code, notes, and snippets.

@jac18281828
Created May 20, 2025 19:10
Show Gist options
  • Save jac18281828/2180b5aa63365cfd727f02d9d41bb723 to your computer and use it in GitHub Desktop.
Save jac18281828/2180b5aa63365cfd727f02d9d41bb723 to your computer and use it in GitHub Desktop.
Migratory Birds Hackerrank
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