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




/* NetRexx */
options replace format comments java crossref symbols nobinary
 
run_samples()
return
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method mode(lvector = java.util.List) public static returns java.util.List
  seen    = 0
  modes   = ''
  modeMax = 0
  loop v_ = 0 to lvector.size() - 1
    mv = Rexx lvector.get(v_)
    seen[mv] = seen[mv] + 1
    select
      when seen[mv] > modeMax then do
        modeMax = seen[mv]
        modes = ''
        nx = 1
        modes[0]  = nx
        modes[nx] = mv
        end
      when seen[mv] = modeMax then do
        nx = modes[0] + 1
        modes[0]  = nx
        modes[nx] = mv
        end
      otherwise do
        nop
        end
      end
    end v_
 
  modeList = ArrayList(modes[0])
  loop v_ = 1 to modes[0]
    val = modes[v_]
    modeList.add(val)
    end v_
 
  return modeList
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method mode(rvector = Rexx[]) public static returns java.util.List
  return mode(ArrayList(Arrays.asList(rvector)))
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method show_mode(lvector = java.util.List) public static returns java.util.List
  modes = mode(lvector)
  say 'Vector:' (Rexx lvector).space(0)', Mode(s):' (Rexx modes).space(0)
  return modes
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method show_mode(rvector = Rexx[]) public static returns java.util.List
  return show_mode(ArrayList(Arrays.asList(rvector)))
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method run_samples() public static
  show_mode([Rexx 10, 9, 8, 7, 6, 5, 4, 3, 2, 1])                                   -- 10 9 8 7 6 5 4 3 2 1
  show_mode([Rexx 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0.11])                 -- 0
  show_mode([Rexx 30, 10, 20, 30, 40, 50, -100, 4.7, -11e+2])                       -- 30
  show_mode([Rexx 30, 10, 20, 30, 40, 50, -100, 4.7, -11e+2, -11e+2])               -- 30 -11e2
  show_mode([Rexx 1, 8, 6, 0, 1, 9, 4, 6, 1, 9, 9, 9])                              -- 9
  show_mode([Rexx 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])                               -- 1 2 3 4 5 6 7 8 9 10 11
  show_mode([Rexx 8, 8, 8, 2, 2, 2])                                                -- 8 2
  show_mode([Rexx 'cat', 'kat', 'Cat', 'emu', 'emu', 'Kat'])                        -- emu
  show_mode([Rexx 0, 1, 2, 3, 3, 3, 4, 4, 4, 4, 1, 0])                              -- 4
  show_mode([Rexx 2, 7, 1, 8, 2])                                                   -- 2
  show_mode([Rexx 2, 7, 1, 8, 2, 8])                                                -- 8 2
  show_mode([Rexx 'E', 'n', 'z', 'y', 'k', 'l', 'o', 'p', 'ä', 'd', 'i', 'e'])      -- E n z y k l o p ä d i e
  show_mode([Rexx 'E', 'n', 'z', 'y', 'k', 'l', 'o', 'p', 'ä', 'd', 'i', 'e', 'ä']) -- ä
  show_mode([Rexx 3, 1, 4, 1, 5, 9, 7, 6])                                          -- 1
  show_mode([Rexx 3, 1, 4, 1, 5, 9, 7, 6, 3])                                       -- 3, 1
  show_mode([Rexx 1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17])                              -- 6
  show_mode([Rexx 1, 1, 2, 4, 4])                                                   -- 4 1
  return
 