[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

Function to find statistical mode

I am trying to create a function that will take an input of a list of numbers that are separated by spaces and output the statistical mode.  The mode is the number that occurs most frequently in the set of numbers.

I have tried doing:

$data = array() ;                        // create empty array
$data = explode(" ", $datainput) ;            // brake string into array
$mode = ???


I am not sure how to go through the data and find the value that is repeated most often though.

--
Thanks in advance
0
blainehilton
Asked:
blainehilton
  • 4
  • 4
1 Solution
 
PromethylCommented:
foreach (array_count_vales($data) as $val=>$occurs) {
   if ($occurs > $highestoccurs) {
         $mode = $val;
   }
 
}

echo "The most common (stat mode) is $val with $highestoccurs as the number of occurances.";
0
 
blainehiltonAuthor Commented:
Okay I have:

//$occurs = 0 ;
//$highestoccurs = 0 ;
foreach (array_count_values($data) as $val=>$occurs) {
   if ($occurs > $highestoccurs) {
         $mode = $val;
   }
 
}

and it does not work.  I tried adding the two commented lines at the top to fix PHP notices, but it still gives the wrong mode.

I tried "1 1 1 2 2 2 2 3 3 3" for instance and it says the mode is 2.
0
 
PromethylCommented:
The mode is 2.

Occurances of 1: 3.
Occurrances of 2: 4.
Occurances of 3: 3.

In this example, 2 is your winnder.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
blainehiltonAuthor Commented:
Sorry, bad example.  If I put "1 2 2 3 3 3 4 4" It says the mode is 4.  It seems no matter what it always goes with the last number entered, even if that is not the mode.

The code I am using is:

$datainput = $_POST["datainput"]);
$datainput = trim($datainput) ;

$data = array() ;
$data = explode(" ", $datainput) ;

//$occurs = 0 ;
//$highestoccurs = 0 ;
foreach (array_count_values($data) as $val=>$occurs) {
   if ($occurs > $highestoccurs) {
         $mode = $val;
   }
 
0
 
PromethylCommented:
$datainput = $_POST["datainput"]);
$datainput = trim($datainput) ;

$data = array() ;
$data = explode(" ", $datainput) ;

//$occurs = 0 ;
//$highestoccurs = 0 ;
foreach (array_count_values($data) as $val=>$occurs) {
  $highestoccurs=0;
  if ($occurs > $highestoccurs) {
         $mode = $val;
   }
 
0
 
blainehiltonAuthor Commented:
It still seems to be doing the same thing.
0
 
blainehiltonAuthor Commented:
I raised the points to 200 so perhaps somebody could figure this out?
0
 
PromethylCommented:
Apologies for the delay. This went into the "I think that's right and I'll check it next cycle opportunity" pile.
<?
$_POST["datainput"]="1 2 2 3 3 3 4 4";
$datainput = $_POST["datainput"];
$datainput = trim($datainput) ;

$data = array() ;
$data = explode(" ", $datainput);

$occurs=0 ;
$highestoccurs=0;

foreach (array_count_values($data) as $val=>$occurs) {
  if ($occurs > $highestoccurs) {
         $mode = $val;
         $highestoccurs=$occurs;
   }
}
echo "The statistical mode is $mode with $highestoccurs occurances. ";
?>
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now