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
Finding Most Occurrences of a number
- Jeff Barnes
- Posts: 912
- Joined: Sun Oct 09, 2005 1:05 pm
- Location: Ontario, Canada
- Contact:
- James Bott
- Posts: 4654
- Joined: Fri Nov 18, 2005 4:52 pm
- Location: San Diego, California, USA
- Contact:
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 ]
- Jeff Barnes
- Posts: 912
- Joined: Sun Oct 09, 2005 1:05 pm
- Location: Ontario, Canada
- Contact: