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);
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson