gives the number of distinct elements that appear in list.


applies test to pairs of elements to determine whether they should be considered equivalent.



open allclose all

Basic Examples  (2)

Count distinct elements in a list:

Count distinct values in an association:

Scope  (2)

The list need not have head List:

Treat numbers as equal if they differ by 0.01 or less:

Applications  (1)

Count the number of distinct characters that occur in a string:

Properties & Relations  (3)

CountDistinct[list] is equivalent to CountDistinct[list,SameQ]:

CountDistinct[list] is equivalent to Length[DeleteDuplicates[list]]:

CountDistinct[list,test] is equivalent to Length[DeleteDuplicates[list,test]]:

CountDistinct[list,test] applies test to two elements to determine if they are equivalent:

CountDistinctBy[list,f] applies the transformation f to each element, then compares the results:

Possible Issues  (1)

If the comparison function returns neither True nor False, elements are assumed to be distinct:

Compare with the function that always reports elements as distinct:

Wolfram Research (2014), CountDistinct, Wolfram Language function, (updated 2023).


Wolfram Research (2014), CountDistinct, Wolfram Language function, (updated 2023).


Wolfram Language. 2014. "CountDistinct." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2023.


Wolfram Language. (2014). CountDistinct. Wolfram Language & System Documentation Center. Retrieved from


@misc{reference.wolfram_2024_countdistinct, author="Wolfram Research", title="{CountDistinct}", year="2023", howpublished="\url{}", note=[Accessed: 24-July-2024 ]}


@online{reference.wolfram_2024_countdistinct, organization={Wolfram Research}, title={CountDistinct}, year={2023}, url={}, note=[Accessed: 24-July-2024 ]}