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

Examples

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, https://reference.wolfram.com/language/ref/CountDistinct.html (updated 2024).

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_2024_countdistinct, author="Wolfram Research", title="{CountDistinct}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/CountDistinct.html}", note=[Accessed: 22-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_countdistinct, organization={Wolfram Research}, title={CountDistinct}, year={2024}, url={https://reference.wolfram.com/language/ref/CountDistinct.html}, note=[Accessed: 22-December-2024 ]}