Skip navigation
Currently Being Moderated

Checking for duplicates in a listappend

Mar 9, 2012 4:24 PM

At certain points I add values into a variable using listappend, might be a single digit, might be a valuelist of numbers from a cfquery result.


The question I have is, does anybody know of a simple way to make sure that duplicates are not added to the list?


Apart from looping over the results of the cfquery and then checking each one to see if it is in the list and if not then adding it.


I'm wondering if CF8 has anything built in that I have overlooked?





  • Currently Being Moderated
    Mar 11, 2012 6:44 AM   in reply to ACS LLC has a function called ListDistinct.  Get it.  Then build your list without worrying about duplicates and use this function when you are done.

    Mark as:
  • Currently Being Moderated
    Mar 11, 2012 2:50 PM   in reply to ACS LLC

    If you want to use it as it is, you'll also need teh ListSort function.  It's in the same library.

    Mark as:
  • Currently Being Moderated
    Mar 24, 2012 5:19 AM   in reply to ACS LLC

    ACS LLC, you could translate the custom tag into a function, something like this


    <cffunction name="removeDuplicates" access="public" output="no" returntype="string" hint="This function will take a list and remove all duplicate values from it.">




    list (required): input list

    sort (optional): perform a text-sort on the list?  Yes or No. Default is No.



    <cfargument name="list" required="true" >

    <cfargument name="sort" required="false" default="no">


    <cfset var returnList = "">

    <cfset var temp_list    = "">


    <cfloop list="#arguments.list#" index="i">

        <cfif listfindnocase(temp_list,i) eq 0>

            <cfset temp_list = listappend(temp_list,i)>




    <cfif arguments.sort eq "yes">

        <cfset returnList = listsort(temp_list, "text")>


        <cfset returnList = temp_list>



    <cfreturn returnList>



    <cfset testlist="a,a,1,1,2">


    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points