Simple way to remove duplicate list items in ColdFusion

This is a very old post. This is much simpler with newer versions of CF/Lucee



I still see examples of people going way out of their way to make sure a common list has do duplicate values.

I have seen people loop their list into a new list with a listFind condition to make sure it wasn't already there. I have seen people put their list into an array (some with a loop and arrayAppend, others with listToArray), arraySort that array, then loop that into a new list, again, checking a listFind condition to make sure it doesnt already exist. I have even seen people loop their lists into queryNew() then use a Query of Queries to select only the unique values, then use valueList(qry.values) to get their unique list.

ALL of those are far more complicated than they need to be.

This is an old ColdFusion trick but still the simplest way of accomplishing this task (that I am aware of). You can simply put the list into a structure using the list items as the structKey (yes, even numbers). A structure cannot have two identical keys so the end result is a structure of unique values. Then you can simply use structKeyList() to get a list back out (if you still need a list).

Here is a small example:

<cfset strList = "9,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,0,0,1,1,2,2,3,4,5,6" />

<cfset listStruct = {} />

<cfloop list="#strList#" index="i">
<cfset listStruct[i] = i />

<cfdump var="#structKeyList(listStruct)#" />

CFScript Junkie? Don't worry, I didn't forget about you.

<cfset strList = "9,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,0,0,1,1,2,2,3,4,5,6" />

function listRemoveDupes(inList,delim)
var listStruct = {};
var i = 1;

for(i=1;i<=listlen(inList, delim);i++)
listStruct[listgetat(inList,i)] = listgetat(inList,i);

return structkeylist(listStruct);

<cfdump var="#listRemoveDupes(strList,",")#" />

Nothing to it. The important part is to remember to define your structure AS a structure. For instance, in the above example, if you removed the listStruct = {} definition, listStruct[i] will assume it is an array since our list values are all numeric.

So there it is, old but still effective.. and far less complicated than the previously mentioned methods (especially the QoQ method... eww...

Most Recent Photos