CFLib.org – Common Function Library Project

listCountUnique(lst)

Last updated November 14, 2007

author

Al Everett

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Counts the unique items in a list. Useful when you don't need to de-dupe a list.

Return Values:
Returns a number.

Example:

<cfset myList="1,1,1,2,3,4,4,4,5">
<cfoutput>#listCountUnique(myList)# unique item(s) in a list of #listLen(myList)# item(s).</cfoutput>

Parameters:

Name Description Required
lst List to parse. Yes

Full UDF Source:

/**
 * Count unique items in a list. (Case-sensitive)
 * 
 * @param lst      List to parse. (Required)
 * @return Returns a number. 
 * @author Al Everett (everett.al@gmail.com) 
 * @version 1, November 14, 2007 
 */
function listCountUnique(lst) {
  var i         = 0;
  var delim     = ",";
  var countList = "";
  var lstArray  = "";

  if (arrayLen(arguments) gt 1) delim = arguments[2];
    
  lstArray = listToArray(lst,delim);
    
  for (i = 1; i LTE arrayLen(lstArray); i = i + 1) {
    if (listFind(countList,lstArray[i],delim) eq 0) {
      countList=listAppend(countList,lstArray[i]);
    }
  }

  return listLen(countList);
}

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