ID:179192
 
I'm looking for a simple way to extract the most common element of a list (or elements, if tied).

Thus, for this list:
("red", "green", "black", "red")
I want to return "red" or ("red") // list is okay

And, for this list:
("red", "green", "black", "red", "green")
I want to return ("red","green")

Also, I don't want to change the original list at all.

Any suggestions on a good technique for doing this?

Thanks!
dramstud wrote:
I'm looking for a simple way to extract the most common element of a list (or elements, if tied).

Thus, for this list:
("red", "green", "black", "red")
I want to return "red" or ("red") // list is okay

And, for this list:
("red", "green", "black", "red", "green")
I want to return ("red","green")

Also, I don't want to change the original list at all.

Any suggestions on a good technique for doing this?

As a matter of fact, I have one:
proc/MostCommon(list/things)
var/list/found=list()
var/item
// count the items we find
for(item in things)
// I'm not sure if just ++found[item] would work, so I use if/else
if(item in found) ++found[item]
else found[item]=1
if(found.len<=1) return found
var/n=found[found[1]]
for(var/i=2,i<=found.len,++i)
item=found[i]
// if this item is less common, ax it
if(found[item]<n)
found.Cut(i,i+1)
--i
continue
// if this item is more common, everything else so far goes
if(found[item]>n)
n=found[item]
found.Cut(1,i)
i=1
// otherwise, do nothing
return found

This is untested code but it should work.

Lummox JR