PHP Code:var my_array:Array = new Array("a","c","b","n","b","v","b","b","d","a","s","c","a","b","b","a","s","a","b","c","b","a","b","a");
function count_by_search(data_arr:Array, str:String) {
Array.prototype.copy = Array.prototype.slice;
var arr = data_arr.copy();
var i = 0;
var count = 0;
while (i<arr.length) {
var j = 0;
while (j<arr.length) {
if (arr[j] == str) {
arr.splice(j,1);
count++;
}
j++;
}
i++;
}
if (count>0) {
return "Duplicate found "+str+": "+(count);
} else if (count == 0) {
return "Invalid search";
} else {
return "No duplicate found";
}
}
Array.prototype.count_by_search = function(str) {
Array.prototype.copy = Array.prototype.slice;
var arr = this.copy();
var i = 0;
var count = 0;
while (i<arr.length) {
var j = 0;
while (j<arr.length) {
if (arr[j] == str) {
arr.splice(j,1);
count++;
}
j++;
}
i++;
}
if (count>0) {
return "Duplicate found "+str+": "+(count);
} else if (count == 0) {
return "Invalid search";
} else {
return "No duplicate found";
}
};
var count;
trace("my_array.count_by_search(\"blah\");\n");
count = my_array.count_by_search("v");
trace(count);
count = my_array.count_by_search("a");
trace(count);
count = my_array.count_by_search("l");
trace(count);
trace("\ncount_by_search(my_array, \"blah\");\n");
count = count_by_search(my_array, "a");
trace(count);
count = count_by_search(my_array, "b");
trace(count);
count = count_by_search(my_array, "l");
trace(count);
Instead doing copy/paste of my code, i suggest you to go through it to understand what the code is doing.
I hope you are going to search the number of similar entries and not the duplicate. Resulting duplicate 1 means you have two similar entries. No duplicate means you have only one entry and Invalid data means no such entries found.
marlopax




Reply With Quote