Thanks, but I'm having some trouble. I try to print the mode but there is no value. Here's the code:
while( list( , $Value ) = each($_POST['nums'])) { $NumberHits[$Value]++; } array_multisort($NumberHists, SORT_DESC); $mode = $NumberHits[0]; echo $mode; ----- Original Message ----- From: "Rick Widmer" <[EMAIL PROTECTED]> To: "Stephen" <[EMAIL PROTECTED]>; "PHP List" <[EMAIL PROTECTED]> Sent: Saturday, December 07, 2002 9:14 PM Subject: Re: [PHP] Finding Mode > At 07:12 PM 12/7/02 -0500, Stephen wrote: > >Another math question... How would I find the mode (number that repeats most > >often) of an array? Then, if there isn't a number that repeats most often, > >tell the user that. > > > For each entry in the array, count the number of times a value occurs: > > > while( list( , $Value ) = each( $MyArray )) { > $NumberHits[ $Value ] ++; > } > > sort $NumberHits keeping the key/value pairs together. You can find a sort > function that will do it here: > > http://www.php.net/manual/en/ref.array.php > > > The first or last entry in the sorted array is the mode. If all the > entries have the same number of hits there is no mode. I'm not sure what > the mode of (1, 1, 2, 5, 9, 19, 19) would be, but I'm suspect a good > definition of mode will tell what to do if more than one value ties for > having the most hits. > > Rick > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php