visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: Knowledge Base : How to get the max-occuring value from an array


By: VGR France  Date: 18/03/2005 11:20:59  English  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 English  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) :
[snip data]

Instantaneous sort + search for max occurence
first maximum occuring value is : 5 (14 times)
time elapsed = 2.83599 ms
maximum occuring values (14 occurences) are : 5

Ladwein's pure PHP (array_* functions)
maximum occuring value is : 5
time elapsed = 0.18811 ms


original unsorted array (dimension=1000) :
[snip data]

Instantaneous sort + search for max occurence
first maximum occuring value is : 60 (20 times)
time elapsed = 20.68186 ms
maximum occuring values (20 occurences) are : 60

Ladwein's pure PHP (array_* functions)
maximum occuring value is : 60
time elapsed = 0.93889 ms


original unsorted array (dimension=10000) :
[snip data]

Instantaneous sort + search for max occurence
first maximum occuring value is : 949 (18 times)
time elapsed = 275.9769 ms
maximum occuring values (18 occurences) are : 949

Ladwein's pure PHP (array_* functions)
maximum occuring value is : 949
time elapsed = 12.33411 ms
done.


Do register to be able to answer

EContact
browser fav
page generated in 329.092030 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page