- public static decimal GetMedian(this IEnumerable < int > source) {
- // Create a copy of the input, and sort the copy
- int[] temp = source.ToArray();
- Array.Sort(temp);
- int count = temp.Length;
- if (count == 0) {
- throw new InvalidOperationException("Empty collection");
- } else if (count % 2 == 0) {
- // count is even, average two middle elements
- int a = temp[count / 2 - 1];
- int b = temp[count / 2];
- return (a + b) / 2 m;
- } else {
- // count is odd, return the middle element
- return temp[count / 2];
- }
- }
- public static int GetMode(this IEnumerable < int > list) {
- // Initialize the return value
- int mode =
- default (int);
- // Test for a null reference and an empty list
- if (list != null && list.Count() > 0) {
- // Store the number of occurences for each element
- Dictionary < int, int > counts = new Dictionary < int, int > ();
- // Add one to the count for the occurence of a character
- foreach(int element in list) {
- if (counts.ContainsKey(element))
- counts[element]++;
- else
- counts.Add(element, 1);
- }
- // Loop through the counts of each element and find the
- // element that occurred most often
- int max = 0;
- foreach(KeyValuePair < int, int > count in counts) {
- if (count.Value > max) {
- // Update the mode
- mode = count.Key;
- max = count.Value;
- }
- }
- }
- return mode;
Loading
Rafnas T PPosted Jul 25, 2019, 2:17 AM
Amit MohantyPosted Jul 25, 2019, 2:02 AM
Rajanikant HawaldarPosted Jul 24, 2019, 9:10 PM