CFLib.org – Common Function Library Project

deDupeArray(inputArray)

Last updated April 26, 2012

author

Dave Anderson

Version: 1 | Requires: CF9 | Library: CFMLLib

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:

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:

/**
 * 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;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson