Averages/Mode: Difference between revisions

Content added Content deleted
m (→‎{{header|Q}}: Fixed </lang>.)
m (→‎{{header|Perl 6}}: slightly sub-optimal syntax)
Line 2,180: Line 2,180:
=={{header|Perl 6}}==
=={{header|Perl 6}}==


{{works with|Rakudo|2018.03}}
{{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;
return |%counts.grep(*.value == $max).map(*.key);
%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
.Bag # count elements
.classify(*.value) # group elements with the same count
.classify(*.value) # group elements with the same count
.max(*.key) # get group with the highest 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
);
}
}