Algosim documentation: frequencies

frequencies

Computes the count of each distinct element in a container.

Syntax

Description

If L is a container, then frequencies(L) returns a list of pairs (x, c) where each x is a distinct element in L and the corresponding c the number of times that x occurs in L. Floating-point comparisons are performed without epsilons.

frequencies(L, ε) has the same behaviour, except that floating-point comparisons are made with epsilon ε.

Examples

Alice ≔ ExampleData("Alice in Wonderland") \ 50
ALICE’S ADVENTURES IN WONDERLAND

Lewis Carroll
…
SortBy(frequencies(words(Alice)), (x ↦ −x[2])) \ 20
(the, 1515)
(and, 774)
(to, 717)
(a, 610)
(she, 498)
(of, 494)
(it, 482)
(said, 456)
(I, 400)
(Alice, 385)
(in, 353)
(was, 352)
(you, 308)
(that, 257)
(as, 246)
(her, 243)
(at, 199)
(on, 189)
(had, 177)
(with, 176)
⋮
sort(frequencies(compute(RandomInt(1000000)^2, n, 1, 1000000) @ digits @ first))
(1, 192164)
(2, 146925)
(3, 123873)
(4, 109158)
(5, 98564)
(6, 90439)
(7, 84566)
(8, 79092)
(9, 75219)
frequencies(IdentityMatrix(5))
(1, 5)
(0, 20)

See also