Skip to content

Instantly share code, notes, and snippets.

@caglarorhan
Created February 16, 2021 22:47
Show Gist options
  • Save caglarorhan/7fed316a741de4d989c025b984e1d66f to your computer and use it in GitHub Desktop.
Save caglarorhan/7fed316a741de4d989c025b984e1d66f to your computer and use it in GitHub Desktop.
MigratoryBirdObservation problem from HackerRank
function migratoryBirds(arr=[1,2,3,4,5,4,3,2,1,3,4]) {
let typesOccurs = {};
arr.forEach(item=>{
if(typesOccurs[item]){
typesOccurs[item]=Number(typesOccurs[item])+1
}else{
typesOccurs[item]=1
}
});
console.log(typesOccurs);
let maxes = Object.values(typesOccurs);
maxes = Math.max(...maxes);
console.log(maxes);
let maxEntries = Object.keys(typesOccurs).filter(key=>typesOccurs[key]===maxes);
return Math.min(...maxEntries);
}
migratoryBirds();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment