Created
May 25, 2012 19:24
-
-
Save atuttle/2790024 to your computer and use it in GitHub Desktop.
O(n) implementation of arrayMerge -- returns an array of the two input arrays combined and de-duped
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cffunction name="arrayMerge"> | |
<cfargument name="a" /> | |
<cfargument name="b" /> | |
<cfset var i = 0 /> | |
<cfset var base = StructNew() /> | |
<cfloop list="#arrayToList(a)#" index="i"> | |
<cfset base[i] = "" /> | |
</cfloop> | |
<cfloop from="1" to="#arrayLen(b)#" index="i"> | |
<cfset base[b[i]] = "" /> | |
</cfloop> | |
<cfreturn listToArray(structKeyList(base)) /> | |
</cffunction> | |
<cfset result = arrayMerge( ['a','b','c','d'], ['a','c','z','x'] ) /> | |
<!--- | |
Returns (order not guaranteed): ['a','b','c','d','z','x'] | |
---> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment