Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

coldfusion distinct list

I was wondering if there was an easy way to enforce distinct values in a coldfusion list or array.

Thanks

like image 933
Timothy Ruhle Avatar asked Mar 08 '11 01:03

Timothy Ruhle


2 Answers

<cfset temp = structNew()>
<cfloop list="a,b,c,a,c" index="i">
  <cfset temp[i] = "">
</cfloop>
<cfset distinctList = structKeyList(temp)>

This is the simplest solution I can think of. The cons of this is the order is not preserved, and list items are case insensitive. If you need case insensitivity, use Java's hashset.

like image 142
Henry Avatar answered Sep 21 '22 07:09

Henry


Before adding a value check to see if it exists by using arrayContains or listFindNoCase.

like image 32
Sam Farmer Avatar answered Sep 19 '22 07:09

Sam Farmer