Page 2 of 2
Re: counting occurrences
Posted: Wed Sep 29, 2021 1:33 pm
by Klaus
Hi Michael,
yes, but that does not take doublettes into account what the thread starter asked for.
Best
Klaus
Re: counting occurrences
Posted: Wed Sep 29, 2021 7:03 pm
by RCozens
adventuresofgreg wrote: Mon Nov 05, 2012 2:24 pm
Hi: 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
Hi Greg,
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
This works for the small list I tested. Note: the target value should include the decimal point, but no decimal digits. This assumes your value range starts with the whole number (ie: 3.0-3.25, not 2.75-3.25.
Cheers!
Re: counting occurrences
Posted: Sat Aug 26, 2023 7:33 pm
by Fermin
Code: Select all
function CountOccurrences haystack, needle
set the itemdelimiter to needle
return the number of items in haystack - 1
end CountOccurrences