Averages/Mode: Difference between revisions

Content deleted Content added
m <lang> needs a language
Line 3:
If it is not appropriate or possible to support a general collection, use a vector (array), if possible. If it is not appropriate or possible to support an unspecified value type, use integers.
 
See also: [[Averages/Mean|Mean]], [[Averages/Median|Median]]
 
=={{Header|ActionScript}}==
Line 35:
return modeList;
}</lang>
 
=={{Header|Ada}}==
{{works with|Ada 2005}}
 
mode.ads:
<lang Ada>generic
Line 47:
 
end Mode;</lang>
 
mode.adb:
<lang Ada>with Ada.Containers.Indefinite_Vectors;
Line 151 ⟶ 150:
 
end Mode;</lang>
 
example use:
<lang Ada>with Ada.Text_IO;
Line 179 ⟶ 177:
Ada.Text_IO.New_Line;
end Main;</lang>
{{out}}
 
output:
<pre>Input: 1, 2, 3, 1, 2, 4, 2, 5, 2, 3, 3, 1, 3, 6
Result: 2, 3</pre>
Line 186 ⟶ 183:
=={{Header|AutoHotkey}}==
{{AutoHotkey case}}
 
Source: [http://www.autohotkey.com/forum/post-276175.html#276175 AutoHotkey forum] by Laszlo
<lang autohotkey>MsgBox % Mode("1 2 3")
Line 239 ⟶ 235:
= J%
</lang>
{{out}}
Output:
<pre>Mode(s) of a() = 6
Mode(s) of b() = 1 4</pre>
Line 303 ⟶ 299:
free(vc);
return 0;
}</lang>output<lang>got 2 modes:
{{out}}
<pre>got 2 modes:
value = 6, count = 4
value = 12, count = 4</langpre>
 
=={{header|C++}}==
Line 377 ⟶ 375:
return 0;
}</lang>
{{out}}
Output:
2 3
 
=={{header|C sharp|C#}}==
<lang csharp>using System;
using System;
using System.Collections;
using System.Collections.Generic;
Line 428 ⟶ 425:
sorted (sort-by (comp - freq) distrib)
maxfq (freq (first sorted))]
(map value (take-while #(= maxfq (freq %)) sorted))))</lang>
</lang>
 
== {{header|CoffeeScript}} ==
<lang coffeescript>mode = (arr) ->
mode = (arr) ->
# returns an array with the modes of arr, i.e. the
# elements that appear most often in arr
Line 445 ⟶ 440:
(key for key, cnt of counts when cnt == max)
console.log mode [1, 2, 2, 2, 3, 3, 3, 4, 4]</lang>
</lang>
 
 
== {{header|Common Lisp}} ==
 
The following returns a list of the modes of a sequence as the primary value, and the frequency as the secondary value. E.g., <code>(mode '(a b c d a b c a b))</code> produces <code>(A B)</code> and <code>3</code>. hash-table-options can be used to customize the hash table, e.g., to specify the test by which elements are compared.
<lang lisp>(defun mode (sequence &rest hash-table-options)
Line 487 ⟶ 479:
writeln("Mode: ", mode(data));
}</lang>
{{out}}
Output:
<pre>Mode: [3]
Mode: [2, 3]</pre>
 
=={{header|E}}==
 
<lang e>pragma.enable("accumulator")
def mode(values) {
Line 502 ⟶ 493:
return accum [].asSet() for v => ==maxCount in counts { _.with(v) }
}</lang>
 
<lang e>? mode([1,1,2,2,3,3,4,4,4,5,5,6,6,7,8,8,9,9,0,0,0])
# value: [4, 0].asSet()</lang>
 
In the line "<code>maxCount max= (counts[v] := counts.fetch(v, fn{0}) + 1)</code>", <code>max=</code> is an update-assignment operation like <code>+=</code>. (The parentheses are unnecessary.) A more verbose version would be:
 
<lang e> def newCount := counts.fetch(v, fn { 0 }) + 1
counts[v] := newCount
maxCount := maxCount.max(newCount)</lang>
In for loops, each key and value from the collection are [[Pattern Matching|pattern matched]] against the specified <code><var>key pattern</var> => <var>value pattern</var></code>. In "<code>for v => ==maxCount in counts</code>", the <code>==</code> is a pattern-match operator which fails unless the value examined is equal to the specified value; so this selects only the input values (keys in <code>counts</code>) whose counts are equal to the maximum count.
 
=={{header|Elena}}==
 
<lang elena>#define std'dictionary'*.
#define std'basic'*.
Line 563 ⟶ 549:
'program'output << "mode of (" << anArray1 << ") is (" << aMode1 << ")%n".
'program'output << "mode of (" << anArray2 << ") is (" << aMode2 << ")%n".
].</lang>
</lang>
 
=={{header|Euphoria}}==
Line 606 ⟶ 591:
constant s = { 1, "blue", 2, 7.5, 5, "green", "red", 5, 2, "blue", "white" }
pretty_print(1,mode(s),{3})</lang>
{{out}}
 
Output:
<pre>{
"blue",
Line 633 ⟶ 617:
> mode [1;2;1;3;2;0;0];;
val it : int list = [0; 2; 1]</lang>
 
=={{header|Factor}}==
Factor has the word <code>mode</code> in <code>math.statistics</code> vocabulary.
<lang factor>{ 11 9 4 9 4 9 } mode ! 9 </lang>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
 
For the <tt>Qsort_Module</tt> see [[Sorting_algorithms/Quicksort#Fortran]]
<lang fortran>program mode_test
Line 746 ⟶ 731:
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)
Line 761 ⟶ 743:
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)
Line 774 ⟶ 755:
where (xs, notxs) = partition (== x) l
(best, modes) = modesWithCount notxs</lang>
 
=={{header|GAP}}==
<lang gap>mode := function(v)
Line 784 ⟶ 766:
mode([ 7, 5, 6, 1, 5, 5, 7, 12, 17, 6, 6, 5, 12, 3, 6 ]);
# [ 5, 6 ]</lang>
 
=={{header|Go}}==
<lang go>package main
Line 827 ⟶ 810:
keys.findAll { m[it] == m[keys[0]] }
}</lang>
 
Test:
<lang groovy>def random = new Random()
Line 835 ⟶ 817:
println "${mode(a)} == mode(${a})"
}</lang>
{{out}}
 
Output:
<pre>[pattern] == mode([pattern, pattern, pattern, Lamp, pattern, Fri Oct 28 23:43:20 CDT 2011, java.awt.Color[r=255,g=0,b=0], Lamp, Lamp, Lamp, pattern])
[Lamp] == mode([Lamp, Fri Oct 28 23:43:20 CDT 2011, Lamp, java.awt.Color[r=255,g=0,b=0], 42.0, java.awt.Color[r=255,g=0,b=0], Fri Oct 28 23:43:20 CDT 2011, Lamp, pattern, pattern, 42.0])
Line 850 ⟶ 831:
 
=={{header|Icon}} and {{header|Unicon}}==
The <tt>mode</tt> procedure generates all <i>n</i> mode values if the collection is <i>n</i>-modal.
the collection is <i>n</i>-modal.
 
<lang icon>procedure main(args)
every write(!mode(args))
Line 867 ⟶ 846:
}
end</lang>
{{out|Sample outputs}}
 
Sample outputs:
 
<pre>->am 3 1 4 1 5 9 7 6
1
Line 879 ⟶ 856:
=={{header|J}}==
<lang j>mode=: ~. #~ ( = >./ )@( #/.~ )</lang>
 
Example:
<pre> mode 1 1 2 2 3 3 4 4 4 5 5 6 6 7 8 8 9 9 0 0 0
 
4 0</pre>
<lang> mode 1 1 2 2 3 3 4 4 4 5 5 6 6 7 8 8 9 9 0 0 0
4 0</lang>
 
=={{header|Java}}==
Line 977 ⟶ 952:
=={{header|Mathematica}}==
Built-in function commonest returns a '''list''' of the most common element(s), even is there is only one 'commonest' number. Example for multiple 'commonest' numbers and a single 'commonest' number:
<lang Mathematica> Commonest[{b, a, c, 2, a, b, 1, 2, 3}]
Commonest[{b1, a, c, 2, a, b, 13, 2, 3}]</lang>
Commonest[{1, 3, 2, 3}]
</lang>
gives back:
<lang Mathematica> {b,a,2}
{3}</lang>
{b,a,2}
{3}
</lang>
 
=={{header|MATLAB}}==
Line 1,057 ⟶ 1,028:
else acc)
seen []</lang>
 
# mode [1;3;6;6;6;6;7;7;12;12;17];;
- : int list = [6]
Line 1,065 ⟶ 1,035:
=={{header|Octave}}==
Of course Octave has the <tt>mode</tt> function; but it returns only the "lowest" mode if multiple modes are available.
 
<lang octave>function m = mode2(v)
sv = sort(v);
Line 1,095 ⟶ 1,064:
endwhile
endfunction</lang>
 
<lang octave>a = [1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17];
mode2(a)
Line 1,162 ⟶ 1,130:
=={{header|Perl 6}}==
{{works with|Rakudo|#22 "Thousand Oaks"}}
 
<lang perl6>sub mode (@a) {
my %counts;
Line 1,191 ⟶ 1,158:
(maxi cdar
(by cdr group A) ) ) ) )</lang>
{{out}}
Output:
<pre>: (modes (1 3 6 6 6 6 7 7 12 12 17))
-> (6)
Line 1,203 ⟶ 1,170:
: (modes (1 4 A 3 2 7 1 B B 3 6 2 4 C C 5 2 5 B A 3 2 C 3 5 5 4 C 7 7))
-> (5 C 2 3)</pre>
 
=={{header|PowerShell}}==
<lang PowerShell>$data = @(1,1,1,2,3,4,5,5,6,7,7,7)
$groups = $data | group-object | sort-object count -Descending
$groups | ? {$_.Count -eq $groups[0].Count}</lang>
{{out}}
Output:
<pre>Count Name Group
----- ---- -----
Line 1,268 ⟶ 1,236:
>>> modes((1,1,2,4,4))
[1, 4]</lang>
 
{{works with|Python|2.7+ and 3.1+}}
<lang python>>>> from collections import Counter
Line 1,280 ⟶ 1,247:
>>> modes((1,1,2,4,4))
[1, 4]</lang>
 
If you just want one mode (instead of all of them), here's a one-liner for that:
<lang python>def onemode(values):
Line 1,302 ⟶ 1,268:
 
=={{header|REXX}}==
<lang rexx>/*REXX program to find the mode of a vector. */
<lang rexx>
/*REXX program to find the mode of a vector. */
 
/*--------vector---------- ---show vector--- ----show result------ */
Line 1,348 ⟶ 1,313:
end
 
return ?</lang>
{{out}}
</lang>
<pre>
Output:
<pre style="height:30ex;overflow:scroll">
vector=1 8 6 0 1 9 4 6 1 9 9 9
mode=9
Line 1,394 ⟶ 1,358:
p mode_one_pass([1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17]) # => [6]
p mode_one_pass([1, 1, 2, 4, 4]) # => [1, 4]</lang>
 
{{works with|Ruby|1.8.7}}
 
If you just want one mode (instead of all of them), here's a one-liner for that:
<lang ruby>def one_mode(ary)
Line 1,404 ⟶ 1,366:
=={{header|Scala}}==
{{works with|Scala|2.8}}
 
Receiving any collection is easy. Returning the result in the same collection takes some doing.
 
<lang scala>import scala.collection.breakOut
import scala.collection.generic.CanBuildFrom
Line 1,419 ⟶ 1,379:
 
=={{header|Scheme}}==
 
{{works with|Berkeley Scheme}}
 
<lang scheme>(define (mode collection)
(define (helper collection counts)
Line 1,434 ⟶ 1,392:
 
=={{header|Slate}}==
<lang Slate>s@(Sequence traits) mode
s@(Sequence traits) mode
[| sortedCounts |
sortedCounts: (s as: Bag) sortedCounts.
(sortedCounts mapSelect: [| :count :elem | sortedCounts last count = count]) valueSet
].</lang>
</lang>
 
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
 
This code is able to find the mode of any collection of any kind of object.
<lang smalltalk>OrderedCollection extend [
Line 1,479 ⟶ 1,434:
puts [mode 1 1 2 4 4]; # --> 1 4</lang>
Note that this works for any kind of value.
 
=={{header|UNIX Shell}}==
{{works with|bash|4.0}}
Line 1,498 ⟶ 1,454:
<lang bash>mode 1 2 1 2 a b a b a 2
a 2</lang>
 
=={{header|Ursala}}==
The mode function defined below works on lists of any type and returns a list of the modes. There is no concept of a general collection in Ursala. The algorithm is to partition the list by equality, then partition the classes by their lengths, and then select a representative from each member of the set of classes with the maximum length.
 
The mode function defined below works on lists of any type and
returns a list of the modes. There is no
concept of a general collection in Ursala. The algorithm is to
partition the list by equality, then partition the classes by
their lengths, and then select a representative from each member
of the set of classes with the maximum length.
 
<lang Ursala>#import std
 
Line 1,514 ⟶ 1,464:
 
examples = mode~~ (<1,3,6,6,6,7,7,12,12,17>,<1,1,2,4,4>)</lang>
The function is tested on a pair of lists, one with a unique mode and one with multiple modes.
{{out}}
and one with multiple modes. Here is the output.
<pre>(<6>,<4,1>)</pre>
 
Line 1,525 ⟶ 1,475:
The "mode" item and it's count are displayed on status line.
If there are multiple items with the same count, the smallest one is displayed.
<lang vedit>BOF // Copy all data to a new buffer
<lang vedit>
BOF // Copy all data to a new buffer
Reg_Copy(10, ALL)
Buf_Switch(Buf_Free)
Line 1,550 ⟶ 1,499:
Reg_Copy_Block(10, CP, EOL_pos)
Buf_Quit(OK)
Statline_Message(@10)</lang>
 
</lang>
=={{header|XEmacs Lisp}}==
 
This returns a list of the modes. Any type(s) of data can be passed in, and any "equal" predicate function can be specified.
 
<lang xelisp>(defun mode ( predicate &rest values)
"Finds the mode of all values passed in.
Line 1,573 ⟶ 1,520:
(when (eq count mode-count)
(push value modes))))
modes))</lang>
</lang>