Mode

Routine Summary

Calculates the mode(s) of a list of numbers.

Inputs

L₁ - To store the user-inputted list of numbers

Outputs

L₃ - The mode(s) of the original list of numbers
Ans - The mode(s) of the original list of numbers (alternates)

Variables Used

L₂ - Intermediate calculation storage
X - Weregoose's version only

Calculator Compatibility

TI-83/84/+/SE/CSE/CE

Authors

kg583, Trenly, Weregoose

:ClrList L₂,L₃
:iL₁(1)+sum(L₁=L₁(1→L₂(1
:For(L,2,dim(L₁
:If not(max(L₁(L)=imag(L₂
:iL₁(L)+sum(L₁=L₁(L→L₂(1+dim(L₂
:End
:For(L,1,dim(L₂
:If real(L₂(L))=max(real(L₂
:imag(L₂(L→L₃(1+dim(L₃
:End

The mode of a list is the value that occurs most often within the list. This routine will find the mode and, if there are any ties, will display all modes. Examples:

:{1,2,3,3,4
Mode = {3}
:{5,5,6,6,7
Mode = {5,6}

Alternate Routines

:SortA(L₁
:Repeat min(0≠ΔList(L₂
:{0→L₂
:ΔList(L₁→L₃
:For(θ,1,dim(L₃
:If not(L₃(θ
:L₁(θ→L₂(1+dim(L₂
:End
:L₂→L₁
:End
:ΔList(cumSum(L₂

This version coded by Trenly is slightly shorter than the above routine by kg583 and produces the same output, which is saved in Ans rather than L₃. However, the original list in L₁ is modified in the process, so make sure to backup that list somewhere else if you need it later.

:1+max(abs(L₁→X
:L₁+Ans
:While max(Ans
:Ans→L₁
:SortD(L₁
:L₁augment({0},not(ΔList(L₁
:End
:sum(L₁ xor 0→dim(L₁
:L₁-X

This version written by Weregoose is very similar to Trenly's in form and function. The output is, again, in Ans and lists all modes, but does also modify L₁ in the process. Note the advanced use of the xor function.

.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Noncommercial-No Derivative Works 2.5 License.