In statistics, the mode is the value that occurs the most frequently in a data set or a probability distribution. In some fields, notably education, sample data are often called scores, and the sample mode is known as the modal score. [1]
The code below will find the mode value in a non sorted array.
Save the below component with the name mode.cfc in a directory under wwwroot, in my case I save my components in a directory called classes:
<cfcomponent>
</cfcomponent>
To call this component in your code you have to do the following:
1-Invoke the object:
2- Call the getMode method and forward the array of the values:
Hope this will help.
[1] http://en.wikipedia.org/wiki/Mode_(statistics)
The code below will find the mode value in a non sorted array.
Save the below component with the name mode.cfc in a directory under wwwroot, in my case I save my components in a directory called classes:
<cfcomponent>
- <cffunction name="getMode" access="public" returntype="string">
- <cfargument name="amode" type="array" required="true">
<cfset oldcount=0><cfset oldtemp=0>
<cfloop from="1" to="#arrayLen(amode)-1#" index="i">
- <cfset temp=amode[i]>
<cfset count=1>
<cfloop from="#i+1#" to="#arrayLen(amode)-1#" index="j">
- <cfif temp is amode[j]>
- <cfset count=count+1>
<cfif count GT oldcount>
- <cfset oldtemp=temp>
<cfset oldcount=count>
</cfif>
</cfloop>
</cfloop>
<cfreturn oldtemp>
</cffunction>
</cfcomponent>
To call this component in your code you have to do the following:
1-Invoke the object:
- <cfobject
name="mode"
component="classes.mode">
2- Call the getMode method and forward the array of the values:
- <cfoutput> #mode.getMode(amode1)# </cfoutput>
Hope this will help.
[1] http://en.wikipedia.org/wiki/Mode_(statistics)
Comments
Post a Comment