-3

Im currently coding a statistics calculator for Mean, Median, Mode, Max and Min However I am not able to understand the code required for the mode method, I have a Array called arryval which contains the numbers. How to i filter through this array and return a value at the end

Thanks

jcrozier
  • 1
  • 1

1 Answers1

-1

Using linq you can just group by every element, get the amount of elements in every group and then get the element with the max amount of elements

var groups = x.GroupBy(v => v);
int maxCount = groups.Max(g => g.Count());
int mode = groups.First(g => g.Count() == maxCount).Key;
klancar16
  • 89
  • 4