Finding which list element occurs the most often

Before I switch to LS to do this, I thought I’d ask in here as I feel there could be a really simple and elegant way to do this in formula that I’m overlooking…

I have a list of strings and I’d like to find which string occurs the most often in the list. If there isn’t one which occurs more often than the others, just return the first one, e.g:

cat:dog:mouse:dog would return dog

cat:dog:mouse would return cat

I don’t know beforehand which values are likely to be in the list, so I can’t loop through a list of possible values to do the count.

Anyone got a nice little formula for this?

Thanks,

Emily.

Subject: Finding which list element occurs the most often

Emily,

I really don’t see an @Formula way of doing this, at least not anything that would be simpler than using LS.

regards,

raphael

Subject: Finding which list element occurs the most often

Probably a little crazy…

Copy this to a button to see what you get…

list := “dog” : “cat” : “dog” : “mice” : “rat” : “dog” : “dog” :“dog” : “dog” : “dog” : “dog” :“dog” : “dog” : “dog” : “dog” :“dog” : “dog”;

list1 := @Unique(list);

temp := 0;

temp1 := “”;

@For(n := 1;n <= @Elements(list1);n := n + 1;

       @For(z := 1;z <= @Elements(list);z := z + 1;

            @If(list1[n] = list[z];temp := temp + 1;temp)



         );

        temp1 := @Trim(temp1 : (list1[n] + "~" + @Text(temp)));

        temp := 0

);

temp2 := @Sort(temp1;[CustomSort];@If(@TextToNumber(@Word($A;“~”;2)) < @TextToNumber(@Word($B;“~”;2));-1;@TextToNumber(@Word($A;“~”;2)) > @TextToNumber(@Word($B;“~”;2));1;0));

@Prompt([OkCancelList] : [NoSort];“temp2”;“temp2”;“”;temp2);

MostInlist := @Subset(temp2;-1);

@Prompt([Ok];“How Many”;“There are " + @Word(mostinlist;”~“;2) + " " + @Word(mostinlist;”~";1))

Subject: RE: Finding which list element occurs the most often

Betsy - that’s brilliant! Exactly what I was after - thank you so much!

Emily.