CountDistinct[list]
gives the number of distinct elements that appear in list.
CountDistinct[list,test]
applies test to pairs of elements to determine whether they should be considered equivalent.
CountDistinct
CountDistinct[list]
gives the number of distinct elements that appear in list.
CountDistinct[list,test]
applies test to pairs of elements to determine whether they should be considered equivalent.
Details
- CountDistinct[<|key1val1,key2val2,…|>] gives the number of distinct vali.
- CountDistinct[list] is equivalent to CountDistinct[list,SameQ]. »
Examples
open all close allBasic Examples (2)
Scope (2)
The list need not have head List:
CountDistinct[Hold[1 + 2, 4 - 1, 1 + 2, 2 + 1]]Treat numbers as equal if they differ by 0.01 or less:
CountDistinct[{3.0, 3.1, 3.11, 3.5, 3.51}, Abs[#1 - #2] <= 0.01&]Applications (1)
Properties & Relations (3)
CountDistinct[list] is equivalent to CountDistinct[list,SameQ]:
list = Join[RandomInteger[100, {50}], RandomReal[1, {50}], {x, y, z, π, E}];CountDistinct[list] == CountDistinct[list, SameQ]CountDistinct[list] is equivalent to Length[DeleteDuplicates[list]]:
list = RandomInteger[100, {33}];CountDistinct[list] == Length[DeleteDuplicates[list]]CountDistinct[list,test] is equivalent to Length[DeleteDuplicates[list,test]]:
lastDigitSameQ[x_, y_] := Mod[x, 10] === Mod[y, 10]CountDistinct[list, lastDigitSameQ] == Length[DeleteDuplicates[list, lastDigitSameQ]]CountDistinct[list,test] applies test to two elements to determine if they are equivalent:
CountDistinct[{{a, x}, {a, y}, {b, z}}, First[#] == First[#2]&]CountDistinctBy[list,f] applies the transformation f to each element, then compares the results:
CountDistinctBy[{{a, x}, {a, y}, {b, z}}, First]Related Guides
Text
Wolfram Research (2014), CountDistinct, Wolfram Language function, https://reference.wolfram.com/language/ref/CountDistinct.html (updated 2024).
CMS
Wolfram Language. 2014. "CountDistinct." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2024. https://reference.wolfram.com/language/ref/CountDistinct.html.
APA
Wolfram Language. (2014). CountDistinct. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/CountDistinct.html
BibTeX
@misc{reference.wolfram_2026_countdistinct, author="Wolfram Research", title="{CountDistinct}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/CountDistinct.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_countdistinct, organization={Wolfram Research}, title={CountDistinct}, year={2024}, url={https://reference.wolfram.com/language/ref/CountDistinct.html}, note=[Accessed: 13-June-2026]}