Remove duplicates from a list

I have a list of rows and I need to remove duplicates. I tried several things, for example:

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

I am currently using the free ColdFusion 10 version for developers if this affects things.

List example:

lacunar_DM, Homocysteine, HTN, tobacco, indefinite, Lacunar_DM, homocysteine, tobacco

This was created by adding a static list with dynamic pulling from the 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:

The list before deleting duplicates:

lacunar_DM, Homocysteine, HTN, tobacco, indefinite, Lacunar_DM, homocysteine, tobacco

The list after removing duplicates:

lacunar_DM, Homocysteine, HTN, tobacco, indefinite, Lacunar_DM, homocysteine, tobacco

+6
source share
5 answers

The problem is that the list that you created using cfsavecontent has newlines because each item in this list is on its own line in cfsavecontent . Since the other list you are joining does not have the same spaces, you will not get the correct results.

Generally, it’s best to remove extra spaces from lists in Coldfusion.

Try this instead of using cfsavecontent :

 <cfset lacunar_list = "lacunar_DM,Homocysteine,HTN,Tobacco,undetermined" > <cfset combination = ListAppend(lacunar_list, lacunar)> 
+1
source

I think your problem is that your list contains extra space. “Homocysteine” and “Homocysteine” are not the same values. Similarly, Tobacco and Tobacco are not the same values.

 lacunar_DM, Homocysteine, HTN, Tobacco, undetermined ,lacunar_DM,Homocysteine,Tobacco -----------^-------------^----^--------^------------^------------X------------X 
+8
source

As already mentioned, list items contain extra space. By looking at your list, all elements use _ as spaces, so the simplest solution is to remove the spaces first and then remove the duplicates.

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

If you have valid spaces, I would suggest using Trim() around the fields when compiling the list manually.

 YourList = ListAppend( YourList, Trim( ListItem ) ) 
+5
source

Make sure that each item in the list has spaces separated. " Tobacco" not equal to "Tobacco" and will not be considered a duplicate. In your SQL query, trim the values ​​to make sure there are no leading or trailing spaces. In your static list, make sure there are no spaces between commas.

+4
source

If you do not have CF10, this is my preferred way to remove duplicates from the list.

 <cfset newlist = [] /> <cfloop list="#combination#" index="i"> <cfif NOT arrayFind(newlist,trim(i))> // can also use arrayFindNoCase <cfset arrayAppend(newlist,trim(i))> </cfif> </cfloop> <cfoutput> #arraytolist(newlist)# </cfoutput> 
+2
source

All Articles