Averages/Mode: Difference between revisions

m
Line 605:
 
=={{header|Elena}}==
ELENA 3.4.x:
<lang elena>import system'routines.;
import system'collections.;
import extensions.;
 
extension op
{
modeget Mode()
[{
var aCountMapcountMap := Dictionary .new(0).;
self .forEach(:anItem(item)
[{
aCountMapcountMap[anItemitem] := aCountMapcountMap[anItemitem] + 1
].};
aCountMapcountMap := aCountMap values; countMap.Values.sort(:(p:,n)( => p > n).;
var aMaxmax := aCountMap firstMembercountMap.FirstMember;
^ aCountMapcountMap
.filterBy:(:kv)(aMax => max.equal(kv value.Value));
.selectBy(:kv)(kv key=> kv.Key);
toArray.toArray()
]}
}
 
public program()
{
[
var anArray1array1 := (new int[] {1, 1, 2, 4, 4).};
var anArray2array2 := (new int[] {1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17).};
var anArray3array3 := (new object[] {1, "blue", 2, 7.5r, 5, "green", "red", 5, 2, "blue", "white").};
console
.printLine("mode of (",anArray1array1.asEnumerable(),") is (",anArray1 modearray1.Mode,")");
.printLine("mode of (",anArray2array2.asEnumerable(),") is (",anArray2 modearray2.Mode,")");
.printLine("mode of (",anArray3array3.asEnumerable(),") is (",anArray3 modearray3.Mode,")");
readChar.readChar()
]}</lang>
{{out}}
<pre>
Anonymous user