CFLib.org – Common Function Library Project

ListRemoveDuplicates(lst [, delim])

Last updated August 22, 2005

Version: 1 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 2 time(s). Average Rating: 4.5

Description:
This is a faster alternative to the listDeleteDuplicates() currently in CFLib. It does the same thing, but uses a struct as a set so as to remove the duplicate elements. However, this loses the original order of the list.

Return Values:
Returns a string.

Example:

view plain print about
<cfoutput>#listRemoveDuplicates("a,list,with,a,few,duplicates")#</cfoutput>

Parameters:

Name Description Required
lst List to parse. Yes
delim List delimiter. Defaults to a comma. No

Full UDF Source:

view plain print about
<cfscript>
/**
 * Remove duplicates from a list.
 * 
 * @param lst      List to parse. (Required)
 * @param delim      List delimiter. Defaults to a comma. (Optional)
 * @return Returns a string. 
 * @author Keith Gaughan (keith@digital-crew.com) 
 * @version 1, August 22, 2005 
 */

function listRemoveDuplicates(lst) {
    var i       = 0;
    var delim   = ",";
    var asArray = "";
    var set     = StructNew();

    if (ArrayLen(arguments) gt 1) delim = arguments[2];

    asArray = ListToArray(lst, delim);
    for (i = 1; i LTE ArrayLen(asArray); i = i + 1) set[asArray[i]] = "";

    return structKeyList(set, delim);
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Troy Pullis Troy Pullis added
firstXDayOfMonth
16 day(s) ago

Henry Ho Henry Ho added
arrayMap
29 day(s) ago

Henry Ho Henry Ho added
queryGetRow
a while ago

Tony Felice Tony Felice added
getRowFromQuery
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 44 time(s)

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 8 time(s)

Markus Schneebeli                                 ListRemoveByStri...
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson