Share via


c # list to find the Mode and median

Question

Tuesday, October 24, 2017 8:31 AM

c # list<int> to find the Mode and median

please verify my account

All replies (2)

Tuesday, October 24, 2017 8:45 AM ✅Answered

Check the link given below

https://blogs.msmvps.com/deborahk/linq-mean-median-and-mode/

Ashish Pandey


Wednesday, October 25, 2017 7:38 AM ✅Answered | 1 vote

Hello lctk,

There is a way that meet your requirement without using Linq.

static class ExtendMethod {

        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) / 2m;
            }
            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;
        }
    }

For How to use:

List<int> list = new List<int> { 22, 43, 54, 12, 32, 54, 234, 13, 43, 4, 2, 12, 43, 12, 23, 12 };

  Console.WriteLine(list.GetMedian().ToString());

  Console.WriteLine(list.GetMode().ToString());

Sincerely,

Neil Hu

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].