/** * @test */ public function getModeFromAnArray() { $numbers = array(3, 3, 6, 8, 2, 9, 2, 1, 7, 6, 5, 1, 4, 2, 3, 5, 8, 11, 1, 8, 5, 2, 2, 7, 8, 11, 10, 8, 7); $this->assertEquals(array(2, 8), $this->statistic->getMode($numbers)); array_push($numbers, 8); $this->assertEquals(array(8), $this->statistic->getMode($numbers)); $numbers = array(1, 2, 3, 4, 5, 9, 8, 7, 0, 11); $this->assertEquals(0, count($this->statistic->getMode($numbers))); }
private function mode($values) { $this->json .= '{'; $this->json .= '"value":'; $mode = $this->statistic->getMode($values); if (count($mode) > 0) { $this->json .= '"'; $i = 1; foreach ($mode as $value) { $this->json .= $this->formatNumber($value); if ($i < count($mode)) { $this->json .= '; '; } $i++; } $this->json .= '"'; } else { $this->json .= '"-"'; } $this->json .= '}'; }