[thelist] CF: De-duplicating Arrays
Buffington, Michael
michael.buffington at office.xerox.com
Tue Jul 15 07:52:35 CDT 2003
Frank,
I threw something together real quick and dirty after reading your email that does the job using just arrays. No list conversion or anything. It runs pretty quick, although I haven't compared it to doing arrays to lists then back to arrays again.
Here's a question though: which of the two dupes do you keep if the values in the second dimension are different? My functions below keep the first occurrence of a dupe, and prevent the second from being set to the array. Also, the code only works for 2D arrays here, but could very easily be used for a single dimension. Also, the code for the ArrayFind function is not my own, although I modded it some. I lost track of who did it, but I use it quite often, it's quite handy.
Hope it helps.
<cfscript>
myArr = ArrayNew(2);
myArr[1][1] = "Apples";
myArr[1][2] = "Macintosh";
myArr[2][1] = "Pears";
myArr[2][2] = "Green";
myArr[3][1] = "Oranges";
myArr[3][2] = "Mandarin";
myArr[4][1] = "Pears";
myArr[4][2] = "Japanese";
function ArrayFind(arrayToSearch,valueToFind,dimension){
//a variable for looping
var ii = 0;
//loop through the array, looking for the value
for(ii = 1; ii LTE arrayLen(arrayToSearch); ii = ii + 1){
//if this is the value, return the index
if(NOT compare(arrayToSearch[ii][dimension],valueToFind))
return ii;
}
//if we've gotten this far, it means the value was not found, so return 0
return 0;
}
function arrayDeDupe(arrayToDeDupe,dimension) {
// this could be smarter, detecting total dimensions
arrySansDupe = arrayNew(2);
indexCount = 1;
// loop over the supplied array
for (i=1;i lte ArrayLen(arrayToDeDupe);i=i+1) {
// populate cells only if they don't already exist
if (ArrayFind(arrySansDupe,arrayToDeDupe[i][dimension],dimension) eq 0) {
arrySansDupe[indexCount][dimension] = arrayToDeDupe[i][dimension];
arrySansDupe[indexCount][dimension+1] = arrayToDeDupe[i][dimension+1];
indexCount = indexCount + 1;
}
}
return arrySansDupe;
}
newArry = arrayDeDupe(myArr,1);
</cfscript>
-----Original Message-----
I'm trying to de-duplicate an a 2D array by it's first dimension.
More information about the thelist
mailing list