Averages/Mode: Difference between revisions

→‎{{header|Perl 6}}: update code to be idiomatic rather than literally translated Perl 5, and use object identity instead of string comparison
(→‎{{header|Perl 6}}: update code to be idiomatic rather than literally translated Perl 5, and use object identity instead of string comparison)
Line 1,945:
 
=={{header|Perl 6}}==
 
{{works with|Rakudo|20152016.1208}}
<lang perl6>sub mode (*@a) {
my %counts := @a.Bag;
my $max = %counts.values.max;
++%counts{$_} for @a;
myreturn $max|%counts.grep(*.value == [$max] values %counts).map(*.key);
return |map { .key }, grep { .value == $max }, %counts.pairs;
}</lang>
 
Testing with arrays:
<lang perl6>say mode [1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17];
say mode [1, 1, 2, 4, 4];</lang>
 
{{out}}
<pre>
6
(4 1)
</pre>
 
=={{header|Phix}}==
Anonymous user