Task[edit]
Write a program to find the mode value of a collection.

The case where the collection is empty may be ignored. Care must be taken to handle the case where the mode is non-unique.

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
Tasks for calculating statistical measures



in one go
moving (sliding window)
moving (cumulative)

Mean
Arithmetic

Statistics/Basic
Averages/Arithmetic mean
Averages/Pythagorean means


Averages/Simple moving average




Geometric

Averages/Pythagorean means




Harmonic

Averages/Pythagorean means




Quadratic

Averages/Root mean square




Circular

Averages/Mean angle
Averages/Mean time of day




Median

Averages/Median




Mode

Averages/Mode




Standard deviation

Statistics/Basic



Cumulative standard deviation




$ include "seed7_05.s7i";
 
const proc: createModeFunction (in type: elemType) is func
  begin
 
    const func array elemType: mode (in array elemType: data) is func
      result
        var array elemType: maxElems is 0 times elemType.value;
      local
        var hash [elemType] integer: counts is (hash [elemType] integer).value;
        var elemType: aValue is elemType.value;
        var integer: maximum is 0;
      begin
        for aValue range data do
          if aValue in counts then
            incr(counts[aValue]);
          else
            counts @:= [aValue] 1;
          end if;
          if counts[aValue] > maximum then
            maximum := counts[aValue];
            maxElems := [] (aValue);
          elsif counts[aValue] = maximum then
            maxElems &:= aValue;
          end if;
        end for;
      end func;
 
    const func string: str (in array elemType: data) is func
      result
        var string: stri is "";
      local
        var elemType: anElement is elemType.value;
      begin
        for anElement range data do
          stri &:= " " & str(anElement);
        end for;
        stri := stri[2 ..];
      end func;
 
      enable_output(array elemType);
 
  end func;
 
createModeFunction(integer);
 
const proc: main is func
  begin 
    writeln(mode([] (1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17)));
    writeln(mode([] (1, 1, 2, 4, 4)));
  end func;