A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Finding the most frequently occuring element in a array

Hybrid View

  1. #1
    Senior Member
    Join Date
    Jan 2006
    Posts
    263

    Finding the most frequently occuring element in a array

    How can I find the most frequently occuring element in an array of numbers?
    I'm lost on this one.

  2. #2
    Senior Member
    Join Date
    Jan 2006
    Posts
    263
    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?

  3. #3
    Busy doing nothing Boris the Frog's Avatar
    Join Date
    Jan 2001
    Location
    Derby, UK
    Posts
    305
    //message removed
    Last edited by Boris the Frog; 04-20-2007 at 05:47 AM. Reason: double post!
    --------------------------------------------------
    ‘There is no emoticon to express how I am feeling’ - Comic Book Guy
    There's an effective, simple solution to carbon sequestration... it's called 'coal', so leave it alone!
    There's an effective, simple solution to carbon capture....it's called 'trees', so plant some!

  4. #4
    Busy doing nothing Boris the Frog's Avatar
    Join Date
    Jan 2001
    Location
    Derby, UK
    Posts
    305
    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

    Code:
    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);
    Although this doesn't tell you the MOST occuring item in the array you can now easily detect it from the tmpArray result
    hope this helps
    --------------------------------------------------
    ‘There is no emoticon to express how I am feeling’ - Comic Book Guy
    There's an effective, simple solution to carbon sequestration... it's called 'coal', so leave it alone!
    There's an effective, simple solution to carbon capture....it's called 'trees', so plant some!

  5. #5
    Busy doing nothing Boris the Frog's Avatar
    Join Date
    Jan 2001
    Location
    Derby, UK
    Posts
    305
    You can add this function:
    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");
    }
    and call :
    Code:
    findmostfrequent(tmpArray);
    as the last line of findNumItemsInArray function
    --------------------------------------------------
    ‘There is no emoticon to express how I am feeling’ - Comic Book Guy
    There's an effective, simple solution to carbon sequestration... it's called 'coal', so leave it alone!
    There's an effective, simple solution to carbon capture....it's called 'trees', so plant some!

  6. #6
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    another method of finding the most frequently occuring element in an array
    Code:
     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
    my 2c's -- hth

  7. #7
    Senior Member
    Join Date
    Jan 2006
    Posts
    263
    Thank you both for taking the time to help me. I was able to get it working. So thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center