How to resolve the algorithm Averages/Mode step by step in the Elena programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Averages/Mode step by step in the Elena programming language
Table of Contents
Problem Statement
Write a program to find the mode value of a collection. The case where the collection is empty may be ignored. Care must be taken to handle the case where the mode is non-unique. If it is not appropriate or possible to support a general collection, use a vector (array), if possible. If it is not appropriate or possible to support an unspecified value type, use integers.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Averages/Mode step by step in the Elena programming language
Source code in the elena programming language
import system'routines;
import system'collections;
import extensions;
extension op
{
get Mode()
{
var countMap := Dictionary.new(0);
self.forEach:(item)
{
countMap[item] := countMap[item] + 1
};
countMap := countMap.Values.sort:(p,n => p > n);
var max := countMap.FirstMember;
^ countMap
.filterBy:(kv => max.equal(kv.Value))
.selectBy:(kv => kv.Key)
.toArray()
}
}
public program()
{
var array1 := new int[]{1, 1, 2, 4, 4};
var array2 := new int[]{1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17};
var array3 := new object[]{1, "blue", 2, 7.5r, 5, "green", "red", 5, 2, "blue", "white"};
console
.printLine("mode of (",array1.asEnumerable(),") is (",array1.Mode,")")
.printLine("mode of (",array2.asEnumerable(),") is (",array2.Mode,")")
.printLine("mode of (",array3.asEnumerable(),") is (",array3.Mode,")")
.readChar()
}
You may also check:How to resolve the algorithm Law of cosines - triples step by step in the C# programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the bc programming language
You may also check:How to resolve the algorithm Minimum multiple of m where digital sum equals m step by step in the SETL programming language
You may also check:How to resolve the algorithm Array length step by step in the Modula-3 programming language
You may also check:How to resolve the algorithm Compiler/virtual machine interpreter step by step in the Python programming language