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




PROGRAM MODE_AVG
 
!$INTEGER
 
DIM A[10],B[10],Z[10]
 
PROCEDURE SORT(Z[],P->Z[])
   LOCAL N,FLIPS
   FLIPS=TRUE
   WHILE FLIPS DO
      FLIPS=FALSE
      FOR N=0 TO P-1 DO
        IF Z[N]>Z[N+1] THEN SWAP(Z[N],Z[N+1]) FLIPS=TRUE
      END FOR
   END WHILE
END PROCEDURE
 
PROCEDURE CALC_MODE(Z[],P->MODES$)
   LOCAL I,OCCURRENCE,MAXOCCURRENCE,OLDVAL
   SORT(Z[],P->Z[])
   OCCURENCE=1
   MAXOCCURENCE=0
   OLDVAL=Z[0]
   MODES$=""
   FOR I=1 TO P DO
       IF Z[I]=OLDVAL THEN
           OCCURENCE=OCCURENCE+1
         ELSE
           IF OCCURENCE>MAXOCCURENCE THEN
                MAXOCCURENCE=OCCURENCE
                MODES$=STR$(OLDVAL)
             ELSIF OCCURENCE=MAXOCCURENCE THEN
                MODES$=MODES$+STR$(OLDVAL)
             ELSE
                !$NULL
           END IF
           OCCURENCE=1
       END IF
       OLDVAL=Z[I]
   END FOR
   !check after loop
   IF OCCURENCE>MAXOCCURENCE THEN
       MAXOCCURENCE=OCCURENCE
       MODES$=STR$(OLDVAL)
   ELSIF OCCURENCE=MAXOCCURENCE THEN
        MODES$=MODES$+STR$(OLDVAL)
   ELSE
       !$NULL
   END IF
END PROCEDURE
 
BEGIN
   A[]=(1,3,6,6,6,6,7,7,12,12,17)
   B[]=(1,2,4,4,1)
   PRINT("Modes for array A (1,3,6,6,6,6,7,7,12,12,17)";)
   CALC_MODE(A[],10->MODES$)
   PRINT(MODES$)
   PRINT("Modes for array B (1,2,4,4,1)";)
   CALC_MODE(B[],4->MODES$)
   PRINT(MODES$)
END PROGRAM