How to count occurrences in a list without knowing what items there are?

Gaia_Livingston's icon

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!

Roman Thilenius's icon

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...

Source Audio's icon

if your list contains ints, merge itered items into coll

Gaia_Livingston's icon

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!

Max Patch
Copy patch and select New From Clipboard in Max.

Source Audio's icon

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)

Roman Thilenius's icon

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.