Ask Ben: Removing Duplicate List Items While Maintaining Original List Order

<!--- Create a list with duplicate values. --->
<cfset strList = "1,1,2,3,4,1,3,5,5,1,9,0,1" />
 
<!---
	Create an look up index. This is a struct whose only
	purpose is to provide a fast key-lookup for existing values.
--->
<cfset objExistingKeys = {} />
 
<!---
	We are going to create a new array to hold our "new" list
	values. This will be our ordered copy of the list with only
	unique values.
--->
<cfset arrNewList = [] />
 
<!---
	Now, let's loop over the existing list and build our new
	list IN ORDER with only unique values.
--->
<cfloop
	index="strItem"
	list="#strList#"
	delimiters=",">
 
	<!--- Check to see if this item has been used already. --->
	<cfif NOT StructKeyExists( objExistingKeys, strItem )>
 
		<!---
			This list item is not a key in our look up index
			which means that it has NOT been used in our new
			list yet. Let's add it to our new list array.
		--->
		<cfset ArrayAppend( arrNewList, strItem ) />
 
		<!---
			Add the item to our look up index so that we don't
			use it again on further loop iterations.
		--->
		<cfset objExistingKeys[ strItem ] = true />
 
	</cfif>
 
</cfloop>
 
<!---
	Now that we have copied over all the unique values in
	order to our new list, let's collaps the array down into
	a string list.
--->
<cfset strNewList = ArrayToList( arrNewList ) />
 
<!--- Output the new list. --->
#strNewList#

For Cut-and-Paste