Counting unique elements in a list

后端 未结 5 618
长情又很酷
长情又很酷 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:24

    If order is not important this works:

    map (\xs@(x:_) -> (x, length xs)) . group . sort
    

    group . sort will give you a list of lists where all elements that are equal to each other are grouped into the same sublist (without sort, only consecutive equal elements would be grouped together). The map then turns each sublist into a (element, lengthOfSublist)-tuple.

    If you want to order the result by first occurrence, you can use zip before the sort to add an index to each element, then, after grouping, sort again by that index and then remove the index.

提交回复
热议问题