How can I find the most frequently occuring element in an array of numbers?
I'm lost on this one.
Printable View
How can I find the most frequently occuring element in an array of numbers?
I'm lost on this one.
Like if I had this array
code:
var myNums:Array = [4,65,33,22,4,7,54,98,54,65,4];
Or any numbers 4 is the one occuring the most often how could I write a function to return the most occuring number?
//message removed
this code uses an array called 'testArray' that contains letters, but this just makes illustrating how it works easier - you can replace these with numbers if you want.
The showArray function lists the unique items from testarray, as well as a count for the number of times each item is listed in testArray
Although this doesn't tell you the MOST occuring item in the array you can now easily detect it from the tmpArray resultCode:var testArray:Array = new Array("a", "b", "c", "d", "a", "b", "e", "g");
//
function findNumItemsInArray(findIn:Array) {
//the tmpArray will hold at [0], a list on unique items in findIn array
//tmpArray[x] = a number recording the instance in FindIn array of the number at tmpArray[0][x]
var tmpArray:Array = new Array();
tmpArray[0] = new Array();
//
for (var i = 0; i<findIn.length; i++) {
var str:String = tmpArray[0].toString();
//str needs the "," removed
str = removeDelimiters(str, ",");
//find index of current num in tmpArray[0]
var searchFor = findIn[i];
var indexPos:Number = str.indexOf(searchFor);
if (indexPos == -1) {
//it's not found in the tmpArray, so add it
tmpArray[0].push(searchFor);
tmpArray.push(1);
} else {
//it's been found, so increment the count
tmpArray[indexPos+1]++;
}
}
showArray(tmpArray);
}
function showArray(ar:Array) {
for (var i = 0; i<ar.length; i++) {
trace("Array["+i+"]: "+ar[i]);
}
}
//
function removeDelimiters(str:String, del:String):String {
var strArray:Array = str.split(del);
var newStr:String = "";
for (var i = 0; i<strArray.length; i++) {
newStr += strArray[i];
}
return (newStr);
}
findNumItemsInArray(testArray);
hope this helps:)
You can add this function:
and call :Code:function findmostfrequent(ar:Array) {
//search through the arry from 1 up = find the highest number and return the index from [0] of it
var highest:Array = new Array(0, -1);
for (var i = 1; i<=ar.length; i++) {
if (ar[i] > highest[0]){
highest[0] = ar[i];
highest[1] = i;
}
}
trace("The most frequently occuring item in the array is " + ar[0][highest[1]]);
trace("It occurs " + highest[0] + " times");
}
as the last line of findNumItemsInArray functionCode:findmostfrequent(tmpArray);
another method of finding the most frequently occuring element in an array
my 2c's -- hth :DCode:arr1 = [4,65,33,22,4,7,22,54,98,54,65,4];
arr2 = ['tuba','trombone','saxophone','trombone',
'saxophone','tuba','trombone','tuba','trombone'];
Array.prototype.count = function(element){
var n = 0;
var i = this.length;
while(i--){
if(this[i]==element) n++;
}
return n;
}
trace(arr1.count(4)); // 3
trace(arr1.count(22)); // 2
trace(arr2.count('tuba')); // 3
trace(arr2.count('trombone')); // 4
Thank you both for taking the time to help me. I was able to get it working. So thanks. :)