We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Migratory Birds
Migratory Birds
+ 0 comments public static int migratoryBirds(List<Integer> arr) { List<Integer> typeCountArray=new ArrayList<>(); int countMax=0; for(int i=1;i<6;i++){ int typeCount=0; for(int j=0;j<arr.size();j++){ if(arr.get(j)==i) typeCount++; } if(typeCount>countMax) countMax=typeCount; typeCountArray.add(typeCount); } int type=0; for(int i=0;i<typeCountArray.size()+1;i++){ if(typeCountArray.get(i)==countMax){ type=i+1; break; } } return type; }
+ 0 comments C# solution:
public static int migratoryBirds(List<int> arr) { int result = 0; List<int> x = new List<int>() {0,0,0,0,0}; for (int i=0; i<5; i++) { for (int j=0; j<arr.Count; j++) { if(arr[j] == i+1) { x[i]++; } } } result = Result.maxValMinIndex(x) + 1; return result; } public static int maxValMinIndex(List<int> arr) { int maxVal = 0; int index = 0; for(int i=0; i<arr.Count; i++) { if(i==0 || arr[i] > maxVal) { maxVal = arr[i]; index = i; } } return index; }
+ 0 comments Solution in R:
Note that array indices begin at 1 in R.
migratoryBirds <- function(arr) { t <- table(arr) mode <- names(t[t == max(t)]) return(mode[1]) }
+ 0 comments //Java 8 Map map=new HashMap<>(); for(int i = 0;i
+ 0 comments JavaScript solution:
function migratoryBirds(arr) { // only birds type 1 - 5. create an array of zeros, and remember // arrays start at 0. so bird type 4 would be birdsCount[3]. const birdsCount = new Array(5).fill(0); // loop through arr and increment count based on arr[i] - 1. // why arr[i] - 1? again, because arrays start at 0. for (let i = 0; i < arr.length; i++) { birdsCount[arr[i] - 1] += 1; } // now determine out of the birdsCount, which one has the largest count? const maxCount = Math.max(...birdsCount); // using maxCount, which index in birdsCount appears first? // remember the index is bird type - 1. cause again, arrays start at 0. const firstIndex = birdsCount.indexOf(maxCount); // using the index found, add 1. // why add 1? look at the first comment. return firstIndex + 1; }
Load more conversations
Sort 3141 Discussions, By:
Please Login in order to post a comment