Created
February 16, 2021 22:47
-
-
Save caglarorhan/7fed316a741de4d989c025b984e1d66f to your computer and use it in GitHub Desktop.
MigratoryBirdObservation problem from 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=[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