How to count occurrences in a list without knowing what items there are?
Hello everyone,
I’m looking for a way to calculate the diversity of a list, and I would like to count how many occurrences of elements there are in a list. For example, I have a list like:
4 5 4 6 7 4 6
and I would like an output that tells me how many times the different values appear. It doesn’t matter which values they are — I’m only interested in how many occurrences there are. I don’t know the input values in advance, nor the size of the list.
For the example above, I would simply like to obtain a list like:
3 1 2 1
(which are the occurrence counts for the distinct elements).
Thank you very much!
if the order of the numbers on the output list does not matter(?), i would start with zl.sort and save quite some tasks compared to the long way...
if your list contains ints, merge itered items into coll


Hi Roman and Source Audio!
Thank you for the answers. I’m there — I’m posting my idea here even if it’s really not elegant at all. Maybe there’s an alternative simply way?
Thanks!
I allready posted simple solution.
If you want to struggle with zl objects...
well that could be a good excercise.
You could try zl. sort - iter - zl. change
to get list of changed/ unchanged values
Then sum all 1 (changed) followed by 0 (same)
after sorting (if it is possible for his aim) i would start comparing list element #1 with the whole list using vexpr, the qsum of that is the amount - and then we also know which list element to compare next against the list.