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
Sort by
recency
|
3519 Discussions
|
Please Login in order to post a comment
java
public static int migratoryBirds(List arr) { // Write your code here int f=0; int b=0; HashSet s=new HashSet<>(arr); Iterator it=s.iterator(); while(it.hasNext()) { int val=(int)it.next(); int a=(int)Collections.frequency(arr,val); if(a>f) { f=a; b=val; } else if(a==f) { if(val
python3
birdsid_count = {} for i in arr: if i in birdsid_count: birdsid_count[i] += 1 else: birdsid_count[i] = 1 max_ids = max(birdsid_count.values()) max_items = [key for key, value in birdsid_count.items() if value == max_ids] results = min(max_items) return results
Hey everyone! I've posted my Java solution for the challenge. Feel free to reach out if you'd like to chat about programming or other topics!
https://github.com/eduardocintra/hacker-rank-solutions/blob/master/src/br/com/eduardocintra/easy/migratorybirds/MigratoryBirds.java
Hurray!! This is my Python solution for the challenge. If anyone wants to talk about programming, contact me!!
https://github.com/arrammanojkumar/PythonHackerRank/blob/master/HackerRankChallenges/problem_solving/migratory_birds.py
My javascript solution. It passes all the test cases, but there aren't that many so I don't how optimal it is and error proof it is.
It is O(n) though.
Explaination: 1. The code essentially gets the frequency of each number in the array and adds it to a map. 2. Then we search through the array to find the key with the value with the highest frequency 3. Then we check to see if that key is miniumum key value with the most frequency in the array.
There is probably some built-in javascript function or some algorithmic techinque that can simplify the code, but it works and it is generally fast. :)