Finding Most Occurrences of a number

Finding Most Occurrences of a number

Postby Jeff Barnes » Mon Nov 06, 2006 5:32 pm

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
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Postby James Bott » Mon Nov 06, 2006 5:59 pm

Code: Select all  Expand view
#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 ]
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Postby Jeff Barnes » Mon Nov 06, 2006 6:15 pm

Thanks James,

This looks like it will do exactly what I need.


Jeff
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 84 guests