Migratory Birds

  • + 0 comments

    C#

    public static int migratoryBirds(List<int> arr)
        {
            int mostFreq = 0;
            int id = 0;
            
            arr.GroupBy(id => id)
                .ToList()
                .ForEach(g => {
                   Console.WriteLine(g.ToList().Count());
                   int birsFre = g.ToList().Count();
                   int currentId = g.First();
                   if(birsFre > mostFreq) {
                        mostFreq = birsFre;
                        id = currentId; 
                   }
                   if(birsFre == mostFreq){
                        id = id < currentId ? id : currentId;
                   }
                });
                
            return id;
        }