Utility Function: ListRemoveDuplicates()

I often use lists behind the scenes, and if I'm looping through and processing stuff, I had to reprocess something if its been duplicated in the list.


<cffunction name="ListRemoveDuplicates" access="public" returntype="string" output="false">
    <cfargument name="lsList" type="string" required="false" default="">
    <cfargument name="delimiters" type="string" required="false" default=",">

    <cfset var lsNewList = "">

    <cfloop list="#arguments.lsList#" index="listItem" delimiters="#arguments.delimiters#">
        <cfif NOT listFind(lsNewList, listItem, arguments.delimiters)>
            <cfset lsNewList = listAppend(lsNewList, listItem, arguments.delimiters)>
        </cfif>
    </cfloop>

    <cfreturn lsNewList>
</cffunction>

This should probably be ListRemoveDups() as is more fitting for CF, but I prefer the longer name

 

Related Blog Entries

Comments are not allowed for this entry.
Jon Hartmann, July 2011

I'm Jon Hartmann and I'm a Javascript fanatic, UX/UI evangelist and former ColdFusion master. I blog about mysterious error messages, user interface design questions, and all things baffling and irksome about programming for the web.

Learn more about me on LinkedIn.