Counting unique elements in a list

后端 未结 5 617
长情又很酷
长情又很酷 2020-12-03 21:48

Is there a straight-forward combination of standard higher-order functions to count the unique elements in a list?

For example the result for

[1, 1,          


        
5条回答
  •  日久生厌
    2020-12-03 22:12

    The simplest thing would be to sort the items into order, use "group" to put them into sub-lists of equal elements, and then count the items in each sub-list.

    map (\xs -> (head xs, length xs)) . group . sort
    

提交回复
热议问题