Coldfusion - Remove Duplicate Value On List

How To Remove Duplicate Value On A List - Coldfusion

coldfusion remove duplicate item value list

Coldfusion remove duplicate item on list


I am working on a project that requires me to find a way on how to remove duplicate values on a list. I searched for the solutions but I found their codes very complicated.

They approached the problem in a very complicated ways (see Ben Nadel's here), crafting coldfusion custom functions to do the job. It may be pretty impressive doing custom functions but it's just a burden to your CF engine. You don't actually need to do this, as you can use coldfusion's built in function to remove duplicate items on a list.

Here's the easiest way on how to remove duplicate value on a list on coldfusion.
<cfset fulllist='1,2,3,1,1,2,4,5,6,4,8,0,9'>
<cfset mylist=''>

<cfloop list="#fulllist#" index="i">
<cfif #listfind(mylist,i)# eq 0>
<cfset mylist=#listprepend(mylist,i)#>
</cfif>
</cfloop>

#mylist#

Our result above will then have a result of:
1,2,3,4,5,6,8,0

We can make it prettier by sorting the values for numeric items.

<cfset mylist_sorted=#listsort(mylist,'numeric')#>

Outputting the result of our new variable 'mylist_sorted' will have the value:
0,1,2,3,4,5,6,8

Well that's it, how I hope that search engines can pick this up to help other CF programmers out there.

This is another keywordspeak dot com public service post.

2 comments :

  1. well, i tried it with a 3K list and it worked out fine. but i stand corrected to the # correction. thanks! anyway, ben is a great cf developer.

    ReplyDelete
  2. Hey, this looks neat and easy. Thanks for sharing!

    ReplyDelete