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, 4, 0, 4, 4]
would be something like
[(1,2), (4,3), (0,1)]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If order is not important this works:
group . sortwill 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). Themapthen turns each sublist into a(element, lengthOfSublist)-tuple.If you want to order the result by first occurrence, you can use
zipbefore the sort to add an index to each element, then, after grouping, sort again by that index and then remove the index.