CFLib.org – Common Function Library Project

deDupeArray(inputArray)

Last updated April 26, 2012

Version: 1 | Requires: ColdFusion 9 | Library: CFMLLib

 
Rated 0 time(s). Average Rating: 0

Description:
I saw a function here for removing dupes from lists and queries, but not one for arrays. Here's one I've been using for a while.

Return Values:
Returns an array.

Example:

view plain print about
myArray = ["1","2","1","3","1","4"];
myArray = deDupeArray(myArray);

result: ["1","2","3","4"]

Parameters:

Name Description Required
inputArray Array to dedupe. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Removes duplicate values from an array.
 * 
 * @param inputArray      Array to dedupe. (Required)
 * @return Returns an array. 
 * @author Dave Anderson (the.one.true.dave.anderson@gmail.com) 
 * @version 1, April 26, 2012 
 */

public array function deDupeArray(required array inputArray) {
    local.arrList = arrayToList(inputArray);
    local.retArr = inputArray;
    for (local.i = arrayLen(inputArray);i gte 1;i=i-1) {
        if (listValueCountNoCase(arrList,inputArray[i]) gt 1) {
            arrayDeleteAt(retArr,i);
            arrList = arrayToList(retArr);
        }
    }
    return retArr;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Adam Cameron Adam Cameron added
composeDateTime
15 day(s) ago

Chris Weller Chris Weller added
convertQueryStri...
a while ago

Greg Nettles Greg Nettles added
arrayDiff
a while ago

Nathan Dintenfass Nathan Dintenfass added
ArrayOfStructsSo...
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 36 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Rachel Lehman deAccent
Rated 5.0, 6 time(s)

Isaac Dealey                                      countArbitraryDa...
Rated 5.0, 5 time(s)

Created by Raymond Camden / Design by Justin Johnson