ColdFusion tip: remove list duplicates

This is a quick ColdFusion snippet that will process a list and remove any duplicates so that only distinct elements remain.  srcList below is the list containing duplicates.  distinctList is the resultant list with duplicates removed.

<cfset distinctList = ""/>
<cfloop list="#srcList#" index="item">
    <cfif NOT ListFind(distinctList, item)>
        <cfset distinctList = distinctList & IIF(distinctList EQ "",DE(""),DE(",")) & item />

Hope this helps someone!

This entry was posted in development and tagged . Bookmark the permalink.

3 Responses to ColdFusion tip: remove list duplicates

  1. Mike says:

    Try this statement in leiu of the cfset above to clean it up a bit.

  2. Jeff Knooren says:

    Try something like this:

    [cfset distinctList = StructNew()
    [cfloop index=”i” list=”#srcList#”
    [cfset distinctList[i] = “”
    [cfset distinctList = ListSort(StructKeyList(distinctList),”Text”)

    NOTE: I removed the proper brackets so it won’t be hidden. The final CFSET puts your srcList back into a list, and sorts it alphabetically.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s