Languages :: PHP :: Knowledge Base : How to get the max-occuring value from an array |
|||
| By: VGR |
Date: 18/03/2005 11:20:59 |
Points: 0 | Status: Answered Quality : Good |
|
Dated 04/04/2003 <?php // // comparison of various max-occuring-value extracting methods // // //VGR04042003 for EE people //VGR04022005 slight modifs for EEE people // error_reporting(E_ALL&~E_NOTICE); //VGR04022005 ADDed include('timer.inc.php'); $iterations=array(100,1000,10000); set_time_limit(120); srand(); //VGR04022005 DELeted microtime() argument passing, as of PHP 4.2.0+ foreach ($iterations as $constMax) { echo '<HR>'; // generate demo data // generate a $constMax-element array with random integer data spread between 1 and $constMax/10 (dense data) $original=array(); for ($i=0;$i<$constMax;$i++) $original[$i]=rand(0,$constMax/10); // overwrite to demonstrate sorting $original=array(1,1,2,3,4,4,5); // display original data echo "original unsorted array (dimension=".count($original).") : "; if ($constMax<=100) { for ($i=0;$i<count($original);$i++) echo "element $i : value ".$original[$i].' '; } else echo '[snip data] '; // else skip ;-) echo " Instantaneous sort + search for max occurence "; TimerStart(); // ################################################################### // ########### 1 tri instantané Vasiljevic ######################### // ################################################################### // instantaneous sort // initialisations $result=array(); // considered initialized to zero values $cMax=$constMax; // maximum expected limit for values' range // "sort" for ($i=0;$i<count($original);$i++) $result[$original[$i]]++; // display results of most-occuring value $locMax=0; for ($i=0;$i<=$cMax;$i++) if ($result[$i]>$locMax) { $locMaxI=$i; $locMax=$result[$i];} $time1=TimerStop(); echo "first maximum occuring value is : $locMaxI (".$locMax.' times) '; // which is also $locMax, of course echo "time elapsed = $time1 ms "; echo "maximum occuring values ($locMax occurences) are : "; for ($i=0;$i<=$cMax;$i++) if ($result[$i]==$locMax) echo "$i "; echo ' '; echo " Ladwein's pure PHP (array_* functions) "; TimerStart(); // ################################################################### // ########### 2 ladwein's solution ######################### // ################################################################### //only for strings and integers! //$words = array(1,2,3,4,5,6,7,4,3,2,4,4,4,4,4,4,4,3,3); $word_counts = array_count_values($original); $result = array_search(max($word_counts),$word_counts); $time2=TimerStop(); echo "maximum occuring value is : $result "; echo "time elapsed = $time2 ms "; } // foreach echo "<HR>done."; ?> |
|||
| By: VGR | Date: 18/03/2005 11:23:33 | Type : Comment |
|
| The results : I've to acknowledge that PHP's built-in functions perform a lot better than the instantaneous sort technique I use, but : - they are hardcoded (compiled) - they can't give you all the values that are max-occuring. original unsorted array (dimension=100) : |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








