CFLib.org – Common Function Library Project

listFindMaxOccurance(list[, delim])

Last updated February 23, 2005

author

Qasim Rasheed

Version: 2 | Requires: CF5 | Library: StrLib

Description:
This function will return the item with the most occurances in a list.

Return Values:
Returns a string.

Example:

<cfset list = "A1,A2,A3,A1,A3,A1,A2,A1,A1">
<cfset temp = listFindMaxOccurance(list)>
<cfdump var="#temp#">

Parameters:

Name Description Required
list The list to check. Yes
delim List delimiter. Defaults to a comma. No

Full UDF Source:

/**
 * This function will return the item with the most occurances in a list.
 * V2 by Raymond Camden
 * 
 * @param list      The list to check. (Required)
 * @param delim      List delimiter. Defaults to a comma. (Optional)
 * @return Returns a string. 
 * @author Qasim Rasheed (qasimrasheed@hotmail.com) 
 * @version 2, February 23, 2005 
 */
function listFindMaxOccurance(list){
    var i = "";
    var delim = ",";
    var maxitem = "";
    var maxcount = 0;
    var thisItem = "";
    if(arrayLen(arguments) gte 2) delim = arguments[2];
        
    for(i=1;i lte listLen(list,delim );i=i+1) {
        thisItem = listGetAt(list,i,delim);
        if(listValueCount(list,thisItem,delim) gt maxcount) {
            maxcount = listValueCount(list,thisItem,delim);
            maxitem = thisItem;
        }
    }
    return maxitem;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson