Skip to content

Instantly share code, notes, and snippets.

@atuttle
Created May 25, 2012 19:24
Show Gist options
  • Save atuttle/2790024 to your computer and use it in GitHub Desktop.
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
<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