Patch Share: Most common item in a list

Alec Gordon's icon

This isn't a question, just posting an algorithm I lost some time to making. Hopefully I can save someone else some time. This is a way to find the most common item in a list, and I'm sure it can be expanded to reform a list by most to least common. If anyone has a more elegant way of doing this, it would be cool to see it.

TFL's icon

Not on my computer right now, but thinking about another, more powerful way, which work with any kind of item (no need to provide a list of what list items can be):

- [zl.iter] 1 through your list
- for each, check if it's in a [coll]. If not, create a new entry with that item in the coll. If yes, add 1 to the corresponding entry. You might need to use [tosymbol] just after the zl.iter in order to treat numbers and symbol items the same way.
At the end you have a [coll] filled more or less like this:
item1 4
item2 1
item3 8
item4 2
- Sort the list by its second column (Which is the number of apparitions of that item) with the message "sort -1 0"
- do whatever you want with that coll (request line N to get the Nth most common item, reconstruct the list with items sorted by number of apparition...)

Will give it a try when I'm back on my computer!

TFL's icon

Also, plenty of other solutions by searching "list most common" in the forum (Patcher examples, externals...).

TFL's icon

It did't went as short as I thought, but it seems to work well!
This can surely be shortened somehow, at the expense of (already poor) readability.

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

Source Audio's icon

coll is also my prefered tool, if unix text tools are not to be used

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

TFL's icon

Nice one, cool way of using "merge"!
I've iterated through your version (dumps only one time at the end, and these [del 10] were creeping me out):

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


Source Audio's icon

I use delays because they guarantee execution of further messages
if objects can't report "done".
using trigger alone is not safe enough for me, as it proved over the years.
I even don't bother to check if timing is ok without them.


Jean-Francois Charles's icon

Variation on the theme, here using the 'max' message to [coll] instead of sorting everything.

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

Source Audio's icon

max message reports only 1 list member, if multiple
have same max number of occurances in the list
that fails....

Jean-Francois Charles's icon

Indeed - merge on the second coll is elegant! (plus there is a bug in the version I posted)

Source Audio's icon

Lobjects had Lmost which also reported only single item
in case multiple items were at top, one with lower 1st index in the list
would be prefered pick.

Roman Thilenius's icon

just to make sure i understand the task.

"most common" means that

1. it must be present in both lists and
2. among those common ones find the one which has the overall highest occurance

right?

and what should happen if there are multiple winners?

Jean-Francois Charles's icon

Alec was just looking for the most common element in a single given list. 2 lists in the patch to serve as test.
The patch given by Source Audio is cool in that it gives the list of all most common elements in case there are several different elements appearing the same amount of times (and the most).