Patch Share: Most common item in a list

    MaxMSP

    Alec Gordon
    Nov 19 2023 | 3:18 am
    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
      TFL
      Nov 19 2023 | 8:03 am
      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!
      Share
    • TFL's icon
      TFL's icon
      TFL
      Nov 19 2023 | 8:06 am
      Also, plenty of other solutions by searching "list most common" in the forum (Patcher examples, externals...).
    • TFL's icon
      TFL's icon
      TFL
      Nov 19 2023 | 4:57 pm
      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 Patcher
      In Max, select New From Clipboard.
      Show Text
    • Source Audio's icon
      Source Audio's icon
      Source Audio
      Nov 20 2023 | 10:08 am
      coll is also my prefered tool, if unix text tools are not to be used
      Max Patcher
      In Max, select New From Clipboard.
    • TFL's icon
      TFL's icon
      TFL
      Nov 20 2023 | 10:39 am
      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 Patcher
      In Max, select New From Clipboard.
    • Source Audio's icon
      Source Audio's icon
      Source Audio
      Nov 20 2023 | 1:40 pm
      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
      Jean-Francois Charles's icon
      Jean-Francois Charles
      Nov 20 2023 | 2:12 pm
      Variation on the theme, here using the 'max' message to [coll] instead of sorting everything.
      Max Patcher
      In Max, select New From Clipboard.
    • Source Audio's icon
      Source Audio's icon
      Source Audio
      Nov 20 2023 | 2:22 pm
      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
      Jean-Francois Charles's icon
      Jean-Francois Charles
      Nov 20 2023 | 3:12 pm
      Indeed - merge on the second coll is elegant! (plus there is a bug in the version I posted)
    • Source Audio's icon
      Source Audio's icon
      Source Audio
      Nov 20 2023 | 3:25 pm
      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
      Roman Thilenius's icon
      Roman Thilenius
      Nov 20 2023 | 11:11 pm
      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
      Jean-Francois Charles's icon
      Jean-Francois Charles
      Nov 21 2023 | 4:04 am
      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).