I was wondering if there was an easy way to enforce distinct values in a coldfusion list or array.
Thanks
<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.
Before adding a value check to see if it exists by using arrayContains or listFindNoCase.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With