Averages/Mode: Difference between revisions
Content added Content deleted
(→{{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: | Line 1,945: | ||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |
||
{{works with|Rakudo| |
{{works with|Rakudo|2016.08}} |
||
<lang perl6>sub mode (*@a) { |
<lang perl6>sub mode (*@a) { |
||
my %counts; |
my %counts := @a.Bag; |
||
my $max = %counts.values.max; |
|||
++%counts{$_} for @a; |
|||
return |%counts.grep(*.value == $max).map(*.key); |
|||
return |map { .key }, grep { .value == $max }, %counts.pairs; |
|||
}</lang> |
}</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}}== |
=={{header|Phix}}== |