ListTools
Occurrences
number of occurrences in a list
Calling Sequence
Parameters
Description
Examples
Occurrences(x, L)
Occurrences(x, L, f, opts)
x
-
anything
L
list
f
(optional) boolean valued procedure
opts
(optional) extra arguments to procedure f
The Occurrences(x, L) command returns the number of entries in the list L which equal x using a boolean comparison.
If a function f is provided, equality is tested using f(x, L[i], opts) for each element Li in the list.
with⁡ListTools:
L≔3.263,2.3,6.255,3.261,3.262
Occurrences⁡3.263,L
1
The number of entries approximately equal to 3.263.
Occurrences⁡3.263,L,verify,float⁡10,digits=3
3
In the following example, the Occurrences command is used in conjunction with the ListTools:-FindRepetitions command to find the mode of a list with alpha-numeric values:
L2≔1,2,3,4,3,4,b,b,a,c,c,c:
SortedList≔sort⁡L2,x,y↦ListTools:−Occurrences⁡y,L2<ListTools:−Occurrences⁡x,L2
SortedList≔c,c,c,b,b,4,3,4,3,a,2,1
ListTools:-FindRepetitions⁡SortedList,ListTools:-Occurrences⁡SortedList1,L2−1
c
See Also
type/list
verify
Download Help Document