LINQ Expression to Find Mode of an Array

by Zoran Horvat

Mode of an array is the element which occurs more often than any other element of that array. We can write a LINQ expression which counts occurrences of every element of the array and then pick the element with largest count.

Below is the function which calculates mode of a collection of integers.

int Mode(IEnumerable<int> collection)
{
    return
        collection
            .GroupBy(value => value)
            .OrderByDescending(group => group.Count())
            .Select(group => group.Key)
            .First();
}

If you are interested in more academic solutions to this same problem, please take a look at the exercise Finding Mode of an Array .

Demonstration

We can use this function in the context of integer arrays to find their mode. Here is the console application which demonstrates the Mode function.

using System;
using System.Collections.Generic;
using System.Linq;

namespace ArrayMode
{

    class Program
    {

        static int Mode(IEnumerable<int> collection)
        {
            return
                collection
                    .GroupBy(value => value)
                    .OrderByDescending(group => group.Count())
                    .Select(group => group.Key)
                    .First();
        }

        static void Print(int[] a)
        {

            for (int i = 0; i < a.Length; i++)
            {
                Console.Write("{0,3}", a[i]);
                if (i < a.Length - 1 && (i + 1) % 10 == 0)
                    Console.WriteLine();
            }
            Console.WriteLine();
            Console.WriteLine();

            var groups = a
                .GroupBy(value => value)
                .OrderBy(group => group.Key);

            foreach (var group in groups)
            {
                Console.WriteLine("{0,3} x {1}", group.Key, group.Count());
            }

        }

        static void Main(string[] args)
        {

            Random rnd = new Random();
            int n = 0;

            while (true)
            {

                Console.Write("Array length (0 to exit): ");
                n = int.Parse(Console.ReadLine());

                if (n <= 0)
                    break;

                int[] a = new int[n];
                for (int i = 0; i < a.Length; i++)
                    a[i] = rnd.Next(9) + 1;

                Print(a);

                int mode = Mode(a);

                Console.WriteLine("Mode = {0}", mode);

                Console.WriteLine();

            }

        }

    }
}

When this application is run, it produces the following output:

            
Array length (0 to exit): 10
  5  7  2  4  9  5  9  7  6  1

  1 x 1
  2 x 1
  4 x 1
  5 x 2
  6 x 1
  7 x 2
  9 x 2
Mode = 5

Array length (0 to exit): 15
  2  5  7  3  3  6  6  3  5  6
  2  4  1  8  8

  1 x 1
  2 x 2
  3 x 3
  4 x 1
  5 x 2
  6 x 3
  7 x 1
  8 x 2
Mode = 3

Array length (0 to exit): 42
  6  2  2  9  6  8  8  8  2  8
  3  1  6  2  8  1  5  2  7  2
  5  6  8  3  7  2  1  4  4  4
  1  6  4  6  4  4  7  7  1  3
  4  6

  1 x 5
  2 x 7
  3 x 3
  4 x 7
  5 x 2
  6 x 7
  7 x 4
  8 x 6
  9 x 1
Mode = 2

Array length (0 to exit): 0
                
    

If you wish to learn more, please watch my latest video courses

About

Zoran Horvat

Zoran Horvat is the Principal Consultant at Coding Helmet, speaker and author of 100+ articles, and independent trainer on .NET technology stack. He can often be found speaking at conferences and user groups, promoting object-oriented and functional development style and clean coding practices and techniques that improve longevity of complex business applications.

  1. Pluralsight
  2. Udemy
  3. Twitter
  4. YouTube
  5. LinkedIn
  6. GitHub