Created
September 23, 2022 15:09
-
-
Save Nasah-Kuma/de053df038d3e650e3910b1a1285f3a8 to your computer and use it in GitHub Desktop.
Hackerrank Challenge: https://www.hackerrank.com/challenges/migratory-birds/problem?isFullScreen=true
This file contains 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) { | |
// Write your code here | |
let countA = 0; | |
let countB = 0; | |
let countC = 0; | |
let countD = 0; | |
let countE = 0; | |
for (let item of arr) { | |
switch (item) { | |
case 1: countA++; | |
break; | |
case 2: countB++; | |
break; | |
case 3: countC++; | |
break; | |
case 4: countD++; | |
break; | |
case 5: countE++; | |
break; | |
default: null; | |
} | |
} | |
const maxVal = Math.max(countA, countB, countC, countD, countE); | |
let countArr = [ {value: 1, count: countA}, {value: 2, count: countB}, {value: 3, count: countC}, {value: 4, count: countD}, {value: 5, count: countE}]; | |
const newCount = countArr.filter(item => item.count === maxVal ); | |
if (newCount.length > 1) { | |
const newArr = newCount.map(item => item.value); | |
return Math.min(...newArr); | |
} else { | |
return newCount[0].value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment