My suggestion is to group the array contents then sort the resulting group collection by the group.Count()
. From the sorted groups select a collection of tuples of type (int Key,int Count)
.Use TakeWhile
to select only the tuples that have the highest count value. Foreach key in the selection, write the value out to the console.
static void Main()
{
int[] collection = new[] { 2, 2, 3, 3, 4, 5 };
var tuples = collection.GroupBy(n => n)
.OrderByDescending(group => group.Count())
.Select(group => (Key: group.Key, Count: group.Count())).ToArray();
var selection = tuples.TakeWhile(x => x.Count == tuples[0].Count);
foreach (var (Key, Count) in selection)
{
Console.WriteLine(Key);
}
}