Skip to main content
Inspiring
August 29, 2006
Question

Deleting duplicate from list

  • August 29, 2006
  • 1 reply
  • 404 views
What is the easy and fastest way to delete duplicate from list.

My dynamic list is pretty small. Need to loop through and delete duplicate and give unique item list.

We don't have a CF function yet?.
    This topic has been closed for replies.

    1 reply

    Participating Frequently
    August 29, 2006
    There's no built-in CF function available for something like this that I am aware of.

    But that's the beauty of user-defined functions, you can create your own! Something like this is very easy to create yourself, this function only took me 2-3 minutes to put together. Try this:

    AdomacroAuthor
    Inspiring
    August 29, 2006
    Thanks...
    What u think about this code?

    <!--- The list with want to clean up --->
    <cfset lst = "a,list,with,a,few,duplicates">
    <!--- Use a struct to build a set of the list elements --->
    <cfset set = StructNew()>
    <cfloop index="elem" list="#lst#">
    <cfset set[elem] = "">
    </cfloop>
    <!--- Convert the set back to a list --->
    <cfset lst = StructKeyList(set)>
    Participating Frequently
    August 29, 2006
    What do I think? Well - I think your code has a few problems:

    1) It's not very re-usable
    2) It's not very easy to read (variables are named poorly)
    3) It's introduces unecessary usage of complex data types (structs)
    4) Functionally, it does not preserve the sort order of the original list.

    To elaborate on #4 above....once you have your list values stored as "keys" in a struct, you lose the original order of the list. Keys in a struct are completely unsorted. A call to StructKeyList will return a list of keys, with no guarantee on the order of the keys returned. The order in which the keys are inserted makes no difference. Once all of the keys are defined in a struct, they are all essentially un-ordered indexes.

    If order is not important to you for this particular usage, than you shouldn't have anything to worry about. Functionally, your code WILL work. But my honest opinion is that there are many "better" ways to approach this (see the example UDF I attached previously).

    Please bear in mind I'm not trying to be mean here, just offering some constructive criticism.