Averages/Mode: Difference between revisions
Content added Content deleted
m (→{{header|Q}}: Fixed </lang>.) |
SqrtNegInf (talk | contribs) m (→{{header|Perl 6}}: slightly sub-optimal syntax) |
||
Line 2,180: | Line 2,180: | ||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |
||
{{works with|Rakudo| |
{{works with|Rakudo|2019.03.1}} |
||
<lang perl6>sub mode (*@a) { |
<lang perl6>sub mode (*@a) { |
||
my %counts := @a.Bag; |
my %counts := @a.Bag; |
||
my $max = %counts.values.max; |
my $max = %counts.values.max; |
||
%counts.grep(*.value == $max).map(*.key); |
|||
} |
} |
||
Line 2,200: | Line 2,200: | ||
<lang perl6>sub mode (*@a) { |
<lang perl6>sub mode (*@a) { |
||
@a.Bag # count elements |
|||
return |(@a |
|||
.classify(*.value) # group elements with the same count |
|||
.max(*.key) # get group with the highest count |
|||
.value.map(*.key); # get elements in the group |
|||
.value.map(*.key); # get elements in the group |
|||
); |
|||
} |
} |
||