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 .
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
In this course, you will learn the basic principles of object-oriented programming, and then learn how to apply those principles to construct an operational and correct code using the C# programming language and .NET.
As the course progresses, you will learn such programming concepts as objects, method resolution, polymorphism, object composition, class inheritance, object substitution, etc., but also the basic principles of object-oriented design and even project management, such as abstraction, dependency injection, open-closed principle, tell don't ask principle, the principles of agile software development and many more.
More...
In this course, you will learn how design patterns can be applied to make code better: flexible, short, readable.
You will learn how to decide when and which pattern to apply by formally analyzing the need to flex around specific axis.
More...
This course begins with examination of a realistic application, which is poorly factored and doesn't incorporate design patterns. It is nearly impossible to maintain and develop this application further, due to its poor structure and design.
As demonstration after demonstration will unfold, we will refactor this entire application, fitting many design patterns into place almost without effort. By the end of the course, you will know how code refactoring and design patterns can operate together, and help each other create great design.
More...
In four and a half hours of this course, you will learn how to control design of classes, design of complex algorithms, and how to recognize and implement data structures.
After completing this course, you will know how to develop a large and complex domain model, which you will be able to maintain and extend further. And, not to forget, the model you develop in this way will be correct and free of bugs.
More...
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.