Count how many different values a list takes in Mathematica

后端 未结 4 1644
梦谈多话
梦谈多话 2021-02-08 02:45

I would like to get the number of different values found in a List.

For example:

The output for the List a={1,2,3,4,5} would be 5 whereas it would b

4条回答
  •  没有蜡笔的小新
    2021-02-08 03:21

    Use DeleteDuplicates (or Union in older versions) to remove duplicate elements. You can then count the elements in the returned list.

    In[8]:= Length[DeleteDuplicates[a]]
    Out[8]= 5
    
    In[9]:= Length[DeleteDuplicates[b]]
    Out[9]= 2
    

提交回复
热议问题