Averages/Mode: Difference between revisions
Content added Content deleted
(New task on finding the Mode of a sample) |
(corrected categorization of task) |
||
Line 1: | Line 1: | ||
{{task| |
{{task|Probability and statistics}}Write a program to find the [[wp:Mode (statistics)|mode]] value of a vector of integers. The case where the vector is empty may be ignored. Care must be taken to handle the case where the mode is non-unique. |
||
See also: [[Mean]], [[Median]] |
See also: [[Mean]], [[Median]] |
Revision as of 13:25, 13 June 2009
Averages/Mode
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
Write a program to find the mode value of a vector of integers. The case where the vector is empty may be ignored. Care must be taken to handle the case where the mode is non-unique.
Tcl
<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>