Averages/Mode: Difference between revisions

m
(Added Algol 68)
imported>Arakov
 
(6 intermediate revisions by 4 users not shown)
Line 379:
SHOW MODEOF []INT( 1, 3, 5, 7, 3, 1, 3, 7, 7, 3, 3 ) ;print( ( newline ) );
SHOW MODEOF []INT( 7, 13, 5, 13, 7, 2, 7, 10, 13 ) ;print( ( newline ) );
SHOW MODEOF []INT( 5 ) ;print( ( newline ) );
# additional test case #
SHOW MODEOF []INT( 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 ) ;print( ( newline )
 
END
Line 390 ⟶ 392:
7 13
5
9
</pre>
 
 
=={{header|APL}}==
Line 937 ⟶ 939:
maxCount := maxCount.max(newCount)</syntaxhighlight>
In for loops, each key and value from the collection are [[Pattern Matching|pattern matched]] against the specified <code><var>key pattern</var> => <var>value pattern</var></code>. In "<code>for v => ==maxCount in counts</code>", the <code>==</code> is a pattern-match operator which fails unless the value examined is equal to the specified value; so this selects only the input values (keys in <code>counts</code>) whose counts are equal to the maximum count.
 
=={{header|EasyLang}}==
<syntaxhighlight>
proc modes . in[] r[] .
r[] = [ ]
for v in in[]
for i to len vals[]
if v = vals[i]
cnt[i] += 1
max = higher cnt[i] max
break 1
.
.
vals[] &= v
cnt[] &= 0
.
for i to len cnt[]
if cnt[i] = max
r[] &= vals[i]
.
.
.
in[] = [ 1 3 6 6 6 6 7 7 12 12 17 ]
modes in[] mods[]
print mods[]
in[] = [ 1 1 2 4 4 ]
modes in[] mods[]
print mods[]
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 959 ⟶ 990:
 
=={{header|Elena}}==
ELENA 56.0x:
<syntaxhighlight lang="elena">import system'routines;
import system'collections;
Line 969 ⟶ 1,000:
{
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()
}
Line 987 ⟶ 1,018:
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()
}</syntaxhighlight>
{{out}}
Line 1,399 ⟶ 1,430:
As of the 2022-07-31 release of Frink, the function can be rewritten as:
<syntaxhighlight lang="frink">modes[vals] := mostCommon[vals]@0</syntaxhighlight>
 
=={{header|FutureBasic}}==
FB has a native function for an array of mode averages.
<syntaxhighlight lang="futurebasic">
local fn ModeAverage( arguments as CFArrayRef ) as CFStringRef
ExpressionRef expRef = fn ExpressionForFunction( @"mode:", @[fn ExpressionForConstantValue( arguments )] )
CFArrayRef modeArray = fn ExpressionValueWithObject( expRef, NULL, NULL )
CFNumberRef number
CFMutableStringRef modeStr = fn MutableStringNew
for number in modeArray
MutableStringAppendFormat( modeStr, @"value = %@\n", number )
next
end fn = modeStr
 
print fn ModeAverage( @[@1, @3, @6, @6, @6, @6, @7, @7, @12, @12, @12, @12, @17] )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
value = 6
value = 12
</pre>
 
 
=={{header|GAP}}==
Line 3,672 ⟶ 3,727:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">class Arithmetic {
static mode(arr) {
var map = {}
Anonymous user