Averages/Mode: Difference between revisions

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 32: Line 32:
</pre>
</pre>


=={{Header|ActionScript}}==
=={{header|ActionScript}}==
This implementation does not work with arbitrary collections. However, it works with arrays containing mixed data, including strings and other arrays.
This implementation does not work with arbitrary collections. However, it works with arrays containing mixed data, including strings and other arrays.
<lang ActionScript>function Mode(arr:Array):Array {
<lang ActionScript>function Mode(arr:Array):Array {
Line 63: Line 63:
}</lang>
}</lang>


=={{Header|Ada}}==
=={{header|Ada}}==
{{works with|Ada 2005}}
{{works with|Ada 2005}}
mode.ads:
mode.ads:
Line 208: Line 208:
Result: 2, 3</pre>
Result: 2, 3</pre>


=={{Header|APL}}==
=={{header|APL}}==
<lang APL>mode←{{s←⌈/⍵[;2]⋄⊃¨(↓⍵)∩{⍵,s}¨⍵[;1]}{⍺,≢⍵}⌸⍵}</lang>
<lang APL>mode←{{s←⌈/⍵[;2]⋄⊃¨(↓⍵)∩{⍵,s}¨⍵[;1]}{⍺,≢⍵}⌸⍵}</lang>


=={{Header|AutoHotkey}}==
=={{header|AutoHotkey}}==
{{AutoHotkey case}}
{{AutoHotkey case}}
Source: [http://www.autohotkey.com/forum/post-276175.html#276175 AutoHotkey forum] by Laszlo
Source: [http://www.autohotkey.com/forum/post-276175.html#276175 AutoHotkey forum] by Laszlo
Line 384: Line 384:
value = 6, count = 4
value = 6, count = 4
value = 12, count = 4</pre>
value = 12, count = 4</pre>

=={{header|C sharp|C#}}==
<lang csharp>using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Test
{
class Program
{
static void Main(string[] args)
{
/*
* We Use Linq To Determine The Mode
*/
List<int> myList = new List<int>() { 1, 1, 2, 4, 4 };

var query = from numbers in myList //select the numbers
group numbers by numbers //group them together so we can get the count
into groupedNumbers
select new { Number = groupedNumbers.Key, Count = groupedNumbers.Count() }; //so we got a query
//find the max of the occurence of the mode
int max = query.Max(g => g.Count);
IEnumerable<int> modes = query.Where(x => x.Count == max).Select(x => x.Number);//match the frequence and select the number
foreach (var item in modes)
{
Console.WriteLine(item);
}
Console.ReadLine();
}

}
}
</lang>


=={{header|C++}}==
=={{header|C++}}==
Line 457: Line 498:
{{out}}
{{out}}
2 3
2 3

=={{header|C sharp|C#}}==
<lang csharp>using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Test
{
class Program
{
static void Main(string[] args)
{
/*
* We Use Linq To Determine The Mode
*/
List<int> myList = new List<int>() { 1, 1, 2, 4, 4 };

var query = from numbers in myList //select the numbers
group numbers by numbers //group them together so we can get the count
into groupedNumbers
select new { Number = groupedNumbers.Key, Count = groupedNumbers.Count() }; //so we got a query
//find the max of the occurence of the mode
int max = query.Max(g => g.Count);
IEnumerable<int> modes = query.Where(x => x.Count == max).Select(x => x.Number);//match the frequence and select the number
foreach (var item in modes)
{
Console.WriteLine(item);
}
Console.ReadLine();
}

}
}
</lang>


=={{header|Clojure}}==
=={{header|Clojure}}==
Line 771: Line 771:
Modes for array B (1,2,4,4,1) 1 4
Modes for array B (1,2,4,4,1) 1 4
</pre>
</pre>

=={{header|Euphoria}}==
=={{header|Euphoria}}==
<lang euphoria>include misc.e
<lang euphoria>include misc.e
Line 1,018: Line 1,019:
2 3
2 3
</pre>
</pre>

=={{header|Haskell}}==
<lang haskell>import Prelude (foldr, maximum, (==), (+))
import Data.Map (insertWith', empty, filter, elems, keys)

mode :: (Ord a) => [a] -> [a]
mode xs = keys (filter (== maximum (elems counts)) counts)
where counts = foldr (\x -> insertWith' (+) x 1) empty xs</lang>
''counts'' is a map from each value found in ''xs'' to the number of occurrences (foldr traverses the list, insertWith' increments the count). This map is then filtered to only those entries whose count is the maximum count, and their keys (the values from the input list) are returned.
> mode [1,2,3,3,2,1,1]
[1]
> mode [1,2,3,3,2,1]
[1,2,3]
Alternately:
<lang haskell>import Data.List (group, sort)

mode :: (Ord a) => [a] -> [a]
mode xs = map fst $ filter ((==best).snd) counts
where counts = map (\l -> (head l, length l)) . group . sort $ xs
best = maximum (map snd counts)</lang>
Another version that does not require an orderable type:
<lang haskell>import Data.List (partition)

mode :: (Eq a) => [a] -> [a]
mode = snd . modesWithCount
where modesWithCount :: (Eq a) => [a] -> (Int, [a])
modesWithCount [] = (0,[])
modesWithCount l@(x:_) | length xs > best = (length xs, [x])
| length xs < best = (best, modes)
| otherwise = (best, x:modes)
where (xs, notxs) = partition (== x) l
(best, modes) = modesWithCount notxs</lang>


=={{header|GAP}}==
=={{header|GAP}}==
Line 1,299: Line 1,268:
[pattern] == mode([42.0, pattern, pattern, Lamp, 42.0, Lamp, Fri Oct 28 23:43:20 CDT 2011, java.awt.Color[r=255,g=0,b=0], pattern, 42.0, pattern])
[pattern] == mode([42.0, pattern, pattern, Lamp, 42.0, Lamp, Fri Oct 28 23:43:20 CDT 2011, java.awt.Color[r=255,g=0,b=0], pattern, 42.0, pattern])
[Lamp, 42.0] == mode([Fri Oct 28 23:43:20 CDT 2011, pattern, Lamp, Lamp, Lamp, java.awt.Color[r=255,g=0,b=0], Fri Oct 28 23:43:20 CDT 2011, 42.0, 42.0, pattern, 42.0])</pre>
[Lamp, 42.0] == mode([Fri Oct 28 23:43:20 CDT 2011, pattern, Lamp, Lamp, Lamp, java.awt.Color[r=255,g=0,b=0], Fri Oct 28 23:43:20 CDT 2011, 42.0, 42.0, pattern, 42.0])</pre>

=={{header|Haskell}}==
<lang haskell>import Prelude (foldr, maximum, (==), (+))
import Data.Map (insertWith', empty, filter, elems, keys)

mode :: (Ord a) => [a] -> [a]
mode xs = keys (filter (== maximum (elems counts)) counts)
where counts = foldr (\x -> insertWith' (+) x 1) empty xs</lang>
''counts'' is a map from each value found in ''xs'' to the number of occurrences (foldr traverses the list, insertWith' increments the count). This map is then filtered to only those entries whose count is the maximum count, and their keys (the values from the input list) are returned.
> mode [1,2,3,3,2,1,1]
[1]
> mode [1,2,3,3,2,1]
[1,2,3]
Alternately:
<lang haskell>import Data.List (group, sort)

mode :: (Ord a) => [a] -> [a]
mode xs = map fst $ filter ((==best).snd) counts
where counts = map (\l -> (head l, length l)) . group . sort $ xs
best = maximum (map snd counts)</lang>
Another version that does not require an orderable type:
<lang haskell>import Data.List (partition)

mode :: (Eq a) => [a] -> [a]
mode = snd . modesWithCount
where modesWithCount :: (Eq a) => [a] -> (Int, [a])
modesWithCount [] = (0,[])
modesWithCount l@(x:_) | length xs > best = (length xs, [x])
| length xs < best = (best, modes)
| otherwise = (best, x:modes)
where (xs, notxs) = partition (== x) l
(best, modes) = modesWithCount notxs</lang>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
Line 1,858: Line 1,859:
<pre>6
<pre>6
1</pre>
1</pre>

=={{header|Oberon-2}}==
=={{header|Oberon-2}}==
{{Works with|oo2c version2}}
{{Works with|oo2c version2}}
Line 1,947: Line 1,949:
5
5
</pre>
</pre>

=={{header|Objeck}}==
=={{header|Objeck}}==
<lang objeck>
<lang objeck>
Line 2,177: Line 2,180:
print "$_ " foreach mode(1, 1, 2, 4, 4);
print "$_ " foreach mode(1, 1, 2, 4, 4);
print "\n";</lang>
print "\n";</lang>

=={{header|Perl 6}}==

{{works with|Rakudo|2019.03.1}}
<lang perl6>sub mode (*@a) {
my %counts := @a.Bag;
my $max = %counts.values.max;
%counts.grep(*.value == $max).map(*.key);
}

# Testing with arrays:
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>

Alternatively, a version that uses a single method chain with no temporary variables: (Same output with same input)

<lang perl6>sub mode (*@a) {
@a.Bag # count elements
.classify(*.value) # group elements with the same count
.max(*.key) # get group with the highest count
.value.map(*.key); # get elements in the group
}

say mode [1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17];
say mode [1, 1, 2, 4, 4];</lang>


=={{header|Phix}}==
=={{header|Phix}}==
Line 2,440: Line 2,412:
[else
[else
(values ms freq)])))</lang>
(values ms freq)])))</lang>

=={{header|Raku}}==
(formerly Perl 6)

{{works with|Rakudo|2019.03.1}}
<lang perl6>sub mode (*@a) {
my %counts := @a.Bag;
my $max = %counts.values.max;
%counts.grep(*.value == $max).map(*.key);
}

# Testing with arrays:
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>

Alternatively, a version that uses a single method chain with no temporary variables: (Same output with same input)

<lang perl6>sub mode (*@a) {
@a.Bag # count elements
.classify(*.value) # group elements with the same count
.max(*.key) # get group with the highest count
.value.map(*.key); # get elements in the group
}

say mode [1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17];
say mode [1, 1, 2, 4, 4];</lang>


=={{header|REXX}}==
=={{header|REXX}}==
Line 2,688: Line 2,692:
ary.max_by { |x| ary.count(x) }
ary.max_by { |x| ary.count(x) }
end</lang>
end</lang>



=={{header|Rust}}==
=={{header|Rust}}==
Line 2,728: Line 2,731:
Mode of vec2 is: [1,4] // may also print [4, 1], vector has no order guarantee
Mode of vec2 is: [1,4] // may also print [4, 1], vector has no order guarantee
</pre>
</pre>



=={{header|S-lang}}==
=={{header|S-lang}}==
Line 2,955: Line 2,957:
2
2
3</pre>
3</pre>
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<lang tcl># Can find the modal value of any vector of values
proc mode {n args} {
foreach n [list $n {*}$args] {
dict incr counter $n
}
set counts [lsort -stride 2 -index 1 -decreasing $counter]
set best {}
foreach {n count} $counts {
if {[lindex $counts 1] == $count} {
lappend best $n
} else break
}
return $best
}

# Testing
puts [mode 1 3 6 6 6 6 7 7 12 12 17]; # --> 6
puts [mode 1 1 2 4 4]; # --> 1 4</lang>
Note that this works for any kind of value.


=={{header|Swift}}==
=={{header|Swift}}==
Line 3,012: Line 2,993:


</lang>
</lang>

=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<lang tcl># Can find the modal value of any vector of values
proc mode {n args} {
foreach n [list $n {*}$args] {
dict incr counter $n
}
set counts [lsort -stride 2 -index 1 -decreasing $counter]
set best {}
foreach {n count} $counts {
if {[lindex $counts 1] == $count} {
lappend best $n
} else break
}
return $best
}

# Testing
puts [mode 1 3 6 6 6 6 7 7 12 12 17]; # --> 6
puts [mode 1 1 2 4 4]; # --> 1 4</lang>
Note that this works for any kind of value.


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
Line 3,056: Line 3,059:
End Sub</lang>{{out}}
End Sub</lang>{{out}}
<pre> 3 4</pre>
<pre> 3 4</pre>

=={{header|Vedit macro language}}==
=={{header|Vedit macro language}}==
Current edit buffer stores the collection. Each line is an item in the collection.
Current edit buffer stores the collection. Each line is an item in the collection.