({{, (RefLink[Count, paclet:ref/Count])[list,pattern]
gives the number of elements in list that match pattern. }, {, (RefLink[Count, paclet:ref/Count])[expr,pattern,levelspec]
gives the total number of subexpressions matching pattern that appear at the levels in expr specified by levelspec. }, {, (RefLink[Count, paclet:ref/Count])[pattern]
represents an operator form of (RefLink[Count, paclet:ref/Count]) that can be applied to an expression.}})

OpenersNotesSection(Spacer1)Details and Options

The first argument to (RefLink[Count, paclet:ref/Count]) need not have head (RefLink[List, paclet:ref/List]).

When used on an (RefLink[Association, paclet:ref/Association]), (RefLink[Count, paclet:ref/Count]) tests only the values of elements, not their keys.

(RefLink[Count, paclet:ref/Count]) uses standard level specifications:

The default value for levelspec in (RefLink[Count, paclet:ref/Count]) is {1}.

A positive level n consists of all parts of expr specified by n indices.

A negative level -n consists of all parts of expr with depth n.

Level -1 consists of numbers, symbols and other objects that do not have subparts.

Level 0 corresponds to the whole expression.

With the option setting (RefLink[Heads, paclet:ref/Heads])->(RefLink[True, paclet:ref/True]), (RefLink[Count, paclet:ref/Count]) looks at heads of expressions and their parts.

(RefLink[Count, paclet:ref/Count])[pattern][expr] is equivalent to (RefLink[Count, paclet:ref/Count])[expr,pattern].

OpenersPrimaryExamplesSection(Spacer1)Examples  (19)

OpenersExampleSection0(Spacer1)Basic Examples  (4)

Count how many times b occurs:

Count[{a,b,a,a,b,c,b},b]

Count powers of x in an (RefLink[Association, paclet:ref/Association]):

Count[<|1->1+x^2,2->x^4,3->a+(1+x^2)^2|>,x^_]

Count powers of x on all levels:

Count[<|1->1+x^2,2->x^4,3->a+(1+x^2)^2|>,x^_,Infinity]

Count symbols:

Count[<|1->1+x^2,2->x^4,3->a+(1+x^2)^2|>,_Symbol,Infinity]

OpenersExampleSection1(Spacer1)Scope  (5)

(RefLink[Count, paclet:ref/Count]) works with patterns:

Count[{a,2,a,a,1,c,b,3,3},_Integer]

Count the number of elements not matching b:

Count[{a,b,a,a,b,c,b,a,a},Except[b]]

Count occurrences of b down to level 2:

Count[{{a,a,b},b,{a,b,a}},b,2]

Count occurrences at level 2 only:

Count[{{a,a,b},b,{a,b,a}},b,{2}]

Count all numeric expressions appearing as part of a larger expression:

Count[x^3+1.5x^2+Pi x +7,_?NumericQ,-1]

A numeric level specification does not include level zero:

Count[5,_?NumericQ,-1]

Use a two-element list to explicitly include level zero:

Count[5,_?NumericQ,{0,-1}]

OpenersExampleSection2(Spacer1)Generalizations & Extensions  (1)

(RefLink[Count, paclet:ref/Count]) works with any head, not just (RefLink[List, paclet:ref/List]):

Count[f[a,b,b,a],b]

OpenersExampleSection3(Spacer1)Options  (1)

OpenersExampleSubsection0(Spacer1)Heads  (1)

By default, expressions appearing as heads are not counted:

Count[Sin[x]+Cos[x]+Sin[x]^2,Sin,-1]

Use the option (RefLink[Heads, paclet:ref/Heads])->(RefLink[True, paclet:ref/True]) to include them:

Count[Sin[x]+Cos[x]+Sin[x]^2,Sin,-1,Heads->True]

OpenersExampleSection4(Spacer1)Applications  (3)

Count the total number of symbols in an expression:

Count[a+b^2/(a+b),_Symbol,Infinity]

Count the total number of elements greater than 0.5:

Count[RandomReal[1,{100}],u_/;u>0.5]

Count the number of rows whose first element is 1:

Count[{{1,2},{2,3},{1,5}},{u_,_}/;u==1]

OpenersExampleSection5(Spacer1)Properties & Relations  (4)

(RefLink[Count, paclet:ref/Count]) returns the length of the result given by (RefLink[Cases, paclet:ref/Cases]):

Count[{1,"f",g,"h","7"},_?StringQ]

Length[Cases[{1,"f",g,"h","7"},_?StringQ]]

(RefLink[Count, paclet:ref/Count]) returns the length of the result given by (RefLink[Position, paclet:ref/Position]):

Count[{1,"f",g,"h","7"},_?StringQ]

Length[Position[{1,"f",g,"h","7"},_?StringQ]]

A count at level {0} is effectively a numericized version of (RefLink[MatchQ, paclet:ref/MatchQ]):

MatchQ[5,_Integer]

Count[5,_Integer,{0}]==1

For most expressions, (RefLink[LeafCount, paclet:ref/LeafCount]) equals the count matching (RefLink[Blank, paclet:ref/Blank])[] at level {-1}:

LeafCount[1+a+b^2]

Count[1+a+b^2,_,{-1},Heads->True]

(RefLink[Count, paclet:ref/Count]) treats (RefLink[Rational, paclet:ref/Rational]) and (RefLink[Complex, paclet:ref/Complex]) as atoms:

AtomQ/@{1/2,1+I}

Count[{1/2,1+I},_,{-1},Heads->True]

(RefLink[LeafCount, paclet:ref/LeafCount]) counts (RefLink[Rational, paclet:ref/Rational]) and (RefLink[Complex, paclet:ref/Complex]) numbers using their (RefLink[FullForm, paclet:ref/FullForm]):

{1/2,1+I}//FullForm

LeafCount[{1/2,1+I}]

OpenersExampleSection6(Spacer1)Possible Issues  (1)

(RefLink[Count, paclet:ref/Count]) looks for matches based on patterns, which may not be the same as numerical equality:

Count[{0.9,.15*6,1.8/2},0.9]

Write a pattern using (RefLink[Condition, paclet:ref/Condition]) to force searching based on numerical equality:

Count[{0.9,.15*6,1.8/2},n_/;n==0.9]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Cases, paclet:ref/Cases])  ▪  (RefLink[Position, paclet:ref/Position])  ▪  (RefLink[LeafCount, paclet:ref/LeafCount])  ▪  (RefLink[FreeQ, paclet:ref/FreeQ])  ▪  (RefLink[MatchQ, paclet:ref/MatchQ])  ▪  (RefLink[MemberQ, paclet:ref/MemberQ])  ▪  (RefLink[Select, paclet:ref/Select])  ▪  (RefLink[SequenceCount, paclet:ref/SequenceCount])  ▪  (RefLink[StringCount, paclet:ref/StringCount])  ▪  (RefLink[BinCounts, paclet:ref/BinCounts])  ▪  (RefLink[Counts, paclet:ref/Counts])  ▪  (RefLink[Tally, paclet:ref/Tally])  ▪  (RefLink[Total, paclet:ref/Total])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Testing and Searching List Elements, paclet:tutorial/TestingAndSearchingListElements])}, {▪(RefLinkPlain[Finding Expressions That Match a Pattern, paclet:tutorial/FindingExpressionsThatMatchAPattern])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Math & Counting Operations on Lists, paclet:guide/MathematicalAndCountingOperationsOnLists])}, {▪(RefLinkPlain[Pattern Matching Functions, paclet:guide/PatternMatchingFunctions])}, {▪(RefLinkPlain[Discrete & Integer Data, paclet:guide/DiscreteAndIntegerData])}, {▪(RefLinkPlain[Computation with Structured Datasets, paclet:guide/ComputationWithStructuredDatasets])}, {▪(RefLinkPlain[List Manipulation, paclet:guide/ListManipulation])}, {▪(RefLinkPlain[Handling Arrays of Data, paclet:guide/HandlingArraysOfData])}, {▪(RefLinkPlain[Rules & Patterns, paclet:guide/RulesAndPatterns])}, {▪(RefLinkPlain[Numerical Data, paclet:guide/NumericalData])}, {▪(RefLinkPlain[Elements of Lists, paclet:guide/ElementsOfLists])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/05-operations-on-lists.html])(WebLink[: Operations on Lists, https://www.wolfram.com/language/elementary-introduction/05-operations-on-lists.html])}})}})