HackerRank: Migratory Birds (Easy)

Link to code challenge

Naive approach.

function migratoryBirds(arr) { let num = Infinity; let count = 0; let largestCount = 0; let newArr = arr.sort(); for (let i = 0; i <= newArr.length; i++) { if (i === 0) { num = newArr[i]; count++; } else if (newArr[i] === newArr[i - 1]) { count++; } else { if (count > largestCount) { largestCount = count; num = newArr[i - 1]; } //DON'T need this else if statement since newArr[i - 1] will always be greater than num as the arr is in numerical order else if (count === largestCount) { if (newArr[i - 1] < num) { num = newArr[i - 1]; } } count = 1; } } return num; }

Comments

Popular posts from this blog

Code Wars: Data Reverse (6 kyu)

Code Wars: longest_palindrome (6 kyu)

Code Wars: Find the odd int