Hi Michael,
yes, but that does not take doublettes into account what the thread starter asked for.
Best
Klaus
counting occurrences
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Re: counting occurrences
Hi Greg,adventuresofgreg wrote: ↑Mon Nov 05, 2012 2:24 pmHi: What do you think would be the fastest and most efficient method of counting the number of occurrences of a number in a list? i
Here's another approach that might produce quicker results depending on the efficiency of the sort command. This assumes your numbers are in the variable, numberList.
Code: Select all
on countItems numberList
put .25 into valueRange
ask "Enter the target value"
put it into targetValue
put targetValue+valueRange into maxValue
sort items numberList ascending numeric
put itemOffset(targetValue,numberList) into firstHit
put 0 into matchCount
if firstHit <> 0 then
repeat with itemNumber = firstHit to the number of items of numberList
if item itemNumber of numberList > maxValue then exit repeat
add 1 to matchCount
end repeat
end if
answer matchCount
end countItems
Cheers!
Rob Cozens dba Serendipity Software Company
Manchester, CA USA
Each new generation gives more attention to the man-made world...
and less attention to the world that made man.
Manchester, CA USA
Each new generation gives more attention to the man-made world...
and less attention to the world that made man.
Re: counting occurrences
Code: Select all
function CountOccurrences haystack, needle
set the itemdelimiter to needle
return the number of items in haystack - 1
end CountOccurrences