Skip to content

Count ​

The number of elements equal to value in the collection.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageCount✓
Count(collection, value)the number of elements equal to value.
Count(QuotientRing(Integers, m))mm exactly, past 2532^{53} too (the quotient-ring-collection patch).
Count(collection, value, level)matches counted down to level (levels 1 through level), or — with level written as {level}\{level\} — at that level only.
Typed signatures
TypePackageApplies
(collection<any>, any?) -> infinity | integercombinatoricsreplaces compute-enginealways
(collection<any>, any?) -> infinity | integercompute-enginealways
(collection<any>, any?) -> infinity | integerresiduesreplaces compute-enginealways

Domain: Collections

Details
  • A value absent from the collection counts as 0.
  • A lazy family's count past 2532^{53} comes back as the exact integer, when its kernel counts in bigint.
  • Tests exact equality against a fixed value — not a Wolfram-style typed pattern like _Integer.
  • See Length for the total element count, and IndexOf for a single matching position.
  • A bare integer level spec counts matches at every level from 1 through it; {level}\{level\} counts that level only.

Examples

6 more kept as data, checked against the oracles
Basic #
live ↗
InCount([1,2,2,3,2],2)\mathrm{Count}(\bigl\lbrack1, 2, 2, 3, 2\bigr\rbrack, 2)Out33
live ↗
InCount([1,2,2,3,2,4],2)\mathrm{Count}(\bigl\lbrack1, 2, 2, 3, 2, 4\bigr\rbrack, 2)Out33

A value absent from the collection counts as 0

live ↗
InCount([1,2,3],9)\mathrm{Count}(\bigl\lbrack1, 2, 3\bigr\rbrack, 9)Out00
live ↗
InCount([a,b,a,a,b,c,b],b)\mathrm{Count}(\bigl\lbrack a, b, a, a, b, c, b\bigr\rbrack, b)Out33
Scope #

The value can itself be a list

live ↗
InCount([[1,2],[1,2],3],[1,2])\mathrm{Count}(\bigl\lbrack\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack1, 2\bigr\rbrack, 3\bigr\rbrack, \bigl\lbrack1, 2\bigr\rbrack)Out22

A predicate counts the elements satisfying it, like Wolfram's pattern test _?(#>2&)\_?(\# > 2 \&)

live ↗
InCount([1,2,3,4],()↦_1⁡>2)\mathrm{Count}(\bigl\lbrack1, 2, 3, 4\bigr\rbrack, ()↦\operatorname{\_1}\gt2)Out22

Only top-level elements are compared by default

live ↗
InCount([[a,a,b],b,[a,b,a]],b)\mathrm{Count}(\bigl\lbrack\bigl\lbrack a, a, b\bigr\rbrack, b, \bigl\lbrack a, b, a\bigr\rbrack\bigr\rbrack, b)Out11

A bare level spec counts matches down through level 2

live ↗
InCount([[a,a,b],b,[a,b,a]],b,2)\mathrm{Count}(\bigl\lbrack\bigl\lbrack a, a, b\bigr\rbrack, b, \bigl\lbrack a, b, a\bigr\rbrack\bigr\rbrack, b, 2)Out33

{2}\{2\} counts matches at level 2 only

live ↗
InCount([[a,a,b],b,[a,b,a]],b,[2])\mathrm{Count}(\bigl\lbrack\bigl\lbrack a, a, b\bigr\rbrack, b, \bigl\lbrack a, b, a\bigr\rbrack\bigr\rbrack, b, \bigl\lbrack2\bigr\rbrack)Out22

Without a value to match, Count gives the cardinality of the collection itself

live ↗
InCount(FactorialNumbers)\mathrm{Count}(\mathrm{FactorialNumbers})Out∞∞
Properties #

The counts of the distinct values add up to the Length

live ↗
InCount([1,2,2,3,3,3],1)+Count([1,2,2,3,3,3],2)+Count([1,2,2,3,3,3],3)=Length([1,2,2,3,3,3])\mathrm{Count}(\bigl\lbrack1, 2, 2, 3, 3, 3\bigr\rbrack, 1)+\mathrm{Count}(\bigl\lbrack1, 2, 2, 3, 3, 3\bigr\rbrack, 2)+\mathrm{Count}(\bigl\lbrack1, 2, 2, 3, 3, 3\bigr\rbrack, 3)=\mathrm{Length}(\bigl\lbrack1, 2, 2, 3, 3, 3\bigr\rbrack)OutTrue\mathrm{True}
Possible issues #

Count tests exact equality; a typed pattern like _Integer matches nothing

live ↗
InCount([1,a,2,b],_Integer⁡)\mathrm{Count}(\bigl\lbrack1, a, 2, b\bigr\rbrack, \operatorname{\_Integer})Out00

Wolfram's _Integer pattern counts every integer element, here 2 (1 and 2).

Count tests exact equality, so a pattern like _Integer\_Integer matches nothing

live ↗
InCount([a,2,a,a,1,c,b,3,3],_Integer⁡)\mathrm{Count}(\bigl\lbrack a, 2, a, a, 1, c, b, 3, 3\bigr\rbrack, \operatorname{\_Integer})Out00

Wolfram's _Integer pattern counts every integer element, here 4 (2, 1, 3, 3).

About
Examples19
Checked againstWolfram Language 10 agree
CrosswalkWolfram Language

See also: Length, IndexOf