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




 
a$ = "1 3 6 6 6 6 7 7 12 12 17"
b$ = "1 2 4 4 1"
 
print "Modes for ";a$
print modes$(a$)
print "Modes for ";b$
print modes$(b$)
 
function modes$(a$)
'get array size
    n=0
    t$ = "*"
    while t$<>""
        n=n+1
        t$=word$(a$, n)
        'print n, t$
    wend
    n=n-1
    'print "n=", n
'dim array
'read in array
    redim a(n)
    for i = 1 to n
        a(i)=val(word$(a$, i))
        'print i, a(i)
    next
'sort
    sort a(), 1, n
'get the modes
    occurence = 1
    maxOccurence = 0
    oldVal = a(1)
    modes$ = ""
    for i = 2 to n
        'print i, a(i)
        if a(i) = oldVal then
            occurence = occurence + 1
        else
            select case
            case occurence >  maxOccurence
                 maxOccurence = occurence
                 modes$ = oldVal; " "
            case occurence = maxOccurence
                modes$ =  modes$; oldVal; " "
            end select
            occurence = 1
        end if
         oldVal = a(i)
    next
    'check after loop
    select case
    case occurence >  maxOccurence
        maxOccurence = occurence
        modes$ = oldVal; " "
    case occurence = maxOccurence
        modes$ =  modes$; oldVal; " "
    end select
end function 