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




sub floor(x)
    return int(x + .05)
end sub
 
SUB ASort$(matriz$())
    local last, gap, first, tempi$, tempj$, i, j
 
    last = arraysize(matriz$(), 1)
 
    gap = floor(last / 10) + 1
    while(TRUE)
	first = gap + 1
	for i = first to last
	    tempi$ = matriz$(i)
	    j = i - gap
	    while(TRUE)
	        tempj$ = matriz$(j)
	 	if (tempi$ >= tempj$) then
	   	    j = j + gap
	   	    break
	 	end if
	 	matriz$(j+gap) = tempj$
	 	if j <= gap then
	   	    break
	 	end if
	 	j = j - gap
	    wend
	    matriz$(j) = tempi$
        next i
	if gap = 1 then
	    return
	else
	    gap = floor(gap / 3.5) + 1
	end if
    wend
END SUB
 
 
sub getMode$(list$) // returns mode and count
	local m$(1), n, i, mode$, count, maxM$, maxC
 
	n = token(list$, m$(), ", ")
	ASort$(m$())
 
	for i = 1 to n
		if m$(i) <> mode$ then
			if count > maxC then
				maxM$ = mode$
				maxC = count
			end if
			count = 1
			mode$ = m$(i)
		else
			count = count + 1
		end if
	next i
 
	return maxM$ + "," + str$(maxC)
end sub
 
result$ = getMode$("1,3,6,6,6,6,7,7,12,12,17")
n = instr(result$, ",")
print "mode ", left$(result$, n - 1), " occur(s) ", right$(result$, len(result$) - n), " times."
 
result$ = getMode$("a, a, b, d, d")
print "mode ", left$(result$, n - 1), " occur(s) ", right$(result$, len(result$) - n), " times."