Sunday, May 28, 2017

#1 2016-09-29 03:18:07 pm

tneison
Member
Registered: 2015-04-02
Posts: 66

Find item occurring most frequently in a list

I created this for error checking a NSCalendar date. Occasionally NSCalendar would return garbage, so I had it run 10 times then used the most prolific value.


Applescript:

set aList to {"a", "a", "a", "b", "c", "a", "b", "c", "a", "e", "f", "a", "a", "a"}

set prolificItem to mostProlificItem(aList)

on mostProlificItem(aList)
   set itemOccuranceList to {}
   repeat with i from 1 to count of aList
       set thisItem to item i of aList
       set x to indexOfItemIfExists(thisItem, itemOccuranceList)
       if x = missing value then
           set end of itemOccuranceList to {thisItem, 1}
       else
           set occuranceItem to item x of itemOccuranceList
           set itemOccuranceCount to item 2 of occuranceItem
           set item x of itemOccuranceList to {thisItem, (itemOccuranceCount + 1)}
       end if
   end repeat
   
   set prolificItem to {missing value, 0}
   repeat with i from 1 to count of itemOccuranceList
       set thisItem to item i of itemOccuranceList
       set thisItemCount to (item 2 of thisItem) as integer
       set currentProlificCount to (item 2 of prolificItem) as integer
       if thisItemCount > currentProlificCount then
           set prolificItem to thisItem
       end if
   end repeat
   
   if item 2 of prolificItem > 1 then
       return item 1 of prolificItem
   else
       return missing value
   end if
end mostProlificItem

on indexOfItemIfExists(thisItem, itemOccuranceList)
   repeat with x from 1 to count of itemOccuranceList
       set occuranceItem to item x of itemOccuranceList
       set itemStrValue to item 1 of occuranceItem
       if itemStrValue = thisItem then
           return x
       end if
   end repeat
   return missing value
end indexOfItemIfExists

Offline

 

#2 2016-10-07 05:18:01 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 4286

Re: Find item occurring most frequently in a list

Nice.  smile

Since it's possible that two or more items might be co-most prolific, and since 'missing value' itself might be the most prolific value in a list, here's a minor variation which returns a list of the most prolific value(s). It also checks for the subject list being empty.

Applescript:

set alist to {}
repeat 100 times
   set end of alist to some item of {random number from 1 to 7, some character of "abcdefg", some item of {"dog", "cat", {aardvark:17}, {1, 2, 3}, missing value, pi, current date}}
end repeat

mostProlificItem(alist)

on mostProlificItem(alist)
   if ((count alist) is 0) then return missing value
   
   set itemOccuranceList to {}
   repeat with i from 1 to count of alist
       set thisItem to item i of alist
       set x to indexOfItemIfExists(thisItem, itemOccuranceList)
       if x = missing value then
           set end of itemOccuranceList to {thisItem, 1}
       else
           set occuranceItem to item x of itemOccuranceList
           set itemOccuranceCount to item 2 of occuranceItem
           set item x of itemOccuranceList to {thisItem, (itemOccuranceCount + 1)}
       end if
   end repeat
   
   -- Modified section.
   set {mostProlific, mostOccurrences} to item 1 of itemOccuranceList
   set mostProlific to {mostProlific}
   repeat with i from 2 to (count itemOccuranceList)
       set {thisItem, thisItemCount} to item i of itemOccuranceList
       if (thisItemCount = mostOccurrences) then
           set end of mostProlific to thisItem
       else if (thisItemCount > mostOccurrences) then
           set {mostProlific, mostOccurrences} to {{thisItem}, thisItemCount}
       end if
   end repeat
   
   if (mostOccurrences > 1) then
       return mostProlific -- or: return {mostProlific, mostOccurrences}
   else
       return missing value
   end if
end mostProlificItem

on indexOfItemIfExists(thisItem, itemOccuranceList)
   repeat with x from 1 to count of itemOccuranceList
       set occuranceItem to item x of itemOccuranceList
       set itemStrValue to item 1 of occuranceItem
       if itemStrValue = thisItem then
           return x
       end if
   end repeat
   return missing value
end indexOfItemIfExists


NG

Offline

 

#3 2016-10-07 09:54:55 am

tneison
Member
Registered: 2015-04-02
Posts: 66

Re: Find item occurring most frequently in a list

Very nice. I wasn't too concerned about a tie in occurrences since, for my use, the error only happens once in a great while, but I'm sure your modifications will come in handy.

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.065 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)