Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing duplicates from a list

I have a list of strings and I need to remove the duplicates. I have tried a number of things, such as:

  • Using listRemoveDuplicates(list,",",true);

  • Using Ben Nadel's approach.

  • Using the ListDeleteDuplicates udf

Unfortunately, none of them worked. I'm really not sure what is going on. So any help would be appreciated.

I am currently using a free Developer's version of ColdFusion 10 in case that affects things.

Sample List:

lacunar_DM, Homocysteine, HTN, Tobacco, undetermined ,lacunar_DM,Homocysteine,Tobacco

This was created by appending a static list with a dynamic one pulled from a database:

<cfsavecontent variable= "lacunar_list">
lacunar_DM,
Homocysteine,
HTN,
Tobacco,
undetermined
</cfsavecontent>
<cfset combination = ListAppend(lacunar_list, lacunar)>

<cfoutput>
List before removing dups: #combination#<br/>
List after removing dups: #listremoveduplicates(combination, ",", true)#<br/>
</cfoutput>

Here are the results:

List before removing dups:

lacunar_DM, Homocysteine, HTN, Tobacco, undetermined ,lacunar_DM,Homocysteine,Tobacco

List after removing dups:

lacunar_DM, Homocysteine, HTN, Tobacco, undetermined ,lacunar_DM,Homocysteine,Tobacco

like image 476
Char Avatar asked Mar 19 '13 16:03

Char


2 Answers

I think your problem is that your list contains extra white space. " Homocysteine" and "Homocysteine" are not the same values. Likewise, " Tobacco" and "Tobacco" are not the same values.

lacunar_DM, Homocysteine, HTN, Tobacco, undetermined ,lacunar_DM,Homocysteine,Tobacco
-----------^-------------^----^--------^------------^------------X------------X
like image 80
genericHCU Avatar answered Sep 28 '22 06:09

genericHCU


As mentioned already, your list items contain extra white space. Looking at your list, all the items are using _ as spaces, so the simplest solution is to remove the spaces first, then remove the duplicates.

listRemoveDuplicates( Replace( YourList, " ", "", "ALL" ) )

If you do have some valid spaces, then I would suggest using a Trim() around the fields when you compile the list manually.

YourList = ListAppend( YourList, Trim( ListItem ) )
like image 30
Busches Avatar answered Sep 28 '22 06:09

Busches