DEV Community

Discussion on: Daily Challenge #220 - What Dominates Your Array?

Collapse
 
andreasjakof profile image
Andreas Jakof • Edited

Not tested (written on the phone) but should do the trick
C#

public static int GetDominator(IEnumerable<int> arr)
    {
        var num = arr.Count()/2;
        return arr.GroupBy( x => x)
            .Where(x => x.Count() > num)
            .OrderByDesc(x => x.Count()) //in case there are more than one
            .FirstOrDefault()? //pick the one most often or null, if there are none 
            .First() ?? -1;

    }