Page 1 of 1

Finding Most Occurrences of a number

Posted: Mon Nov 06, 2006 5:32 pm
by Jeff Barnes
Hi Everybody,

I am trying to find a way to find the most occurrences of a number in a database field ... I do not want to get an avarage.

Ex: 100,95,95,95,100,50,50,60, .... I would want to pick the "95"

Can someone please point me in the right direction.

Thanks,

Jeff

Posted: Mon Nov 06, 2006 5:59 pm
by James Bott

Code: Select all

#include "fivewin.ch"

function main()

   local aItems:= {100,95,95,95,100,50,50,60}

   msgInfo( mostFrequent( aItems ) )

return nil

//-------------------------------------------//

// Return the most frequent item in aItems
function mostFrequent( aItems )
   local aItems, nMax, nCount,nLocation,uLast,i

   aItems:= asort(aItems)
   nMax:=0
   nCount:=0
   nLocation:= 0
   uLast:= aItems[1]

   for i:=2 to len( aItems )
      if aItems[i] == uLast
        nCount++
      else
        nCount:=0
        uLast:= aItems[i]
      endif
      if nCount > nMax
         nMax := nCount
         nLocation:= i
      endif
   next

return aItems[ nLocation ]

Posted: Mon Nov 06, 2006 6:15 pm
by Jeff Barnes
Thanks James,

This looks like it will do exactly what I need.


Jeff