Notice: With the launch of Adobe Cookbooks, this site will no longer be accepting new entries or posting new content. Thanks to everyone who submitted content!

How do I remove repeated values from a list?

The ListDeleteDuplicates(list) UDF found on cflib is a great example of how to accomplish this task.

The UDF works by creating a copy of the list. Before it adds an item to the new list, it checks to see if it doesn't already exist.

<cfscript>
/**
* Case-sensitive function for removing duplicate entries in a list.
* Based on dedupe by Raymond Camden
*
* @param list The list to be modified.
* @return Returns a list.
* @author Jeff Howden (jeff@members.evolt.org)
* @version 1, March 21, 2002
*/
function ListDeleteDuplicates(list) {
var i = 1;
var delimiter = ',';
var returnValue = '';
if(ArrayLen(arguments) GTE 2)
delimiter = arguments[2];
list = ListToArray(list, delimiter);
for(i = 1; i LTE ArrayLen(list); i = i + 1)
if(NOT ListFind(returnValue, list[i], delimiter))
returnValue = ListAppend(returnValue, list[i], delimiter);
return returnValue;
}
</cfscript>

Example of calling this UDF

<cfset myList = "apples,oranges,apples,bananas,ORANGES">
<cfoutput>
List before removing dupes: #myList#<br>
List after removing dupes: #ListDeleteDuplicates(myList)#
</cfoutput>


This question was written by Jeremy Petersen.
It was last updated on December 12, 2006 at 11:56:28 AM EST.

CFML Referenced

<cfscript>
<cfoutput>

Categories

Lists

Comments

Comment made by Dipu B on February 26, 2007 at 11:03 AM
I was thinking about this the other day and thought of doing:

<cfset tmpStruct = StructNew()> <cfloop list="#aList#" index="item"> <cfset tmpStruct["#item#"]="whatever value"> </cfloop> <cfset nodupeList = StructKeyList(tmpStruct)>

Any suggestions, why to do, or why defenitely not, to do it this way is appreciated.


Comment made by Raymond Camden on February 26, 2007 at 3:25 PM
This is a nice way of doing it as well. Probably a bit faster actually.


Comment made by Alex B. on November 1, 2007 at 2:05 PM
The program with StructKeyList by Dipu B. runs at least 5 times faster compared to Jeff Howden ListDeleteDuplicates (sometimes 10 times faster).

However, StructKeyList destroys the original list order. If the order is not importenat, it is better.