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.

Categories

Lists

Comments

comments powered by Disqus