public function mode($key = null) { $count = $this->count(); if ($count == 0) { return; } $collection = isset($key) ? $this->pluck($key) : $this; $counts = new self(); $collection->each(function ($value) use($counts) { $counts[$value] = isset($counts[$value]) ? $counts[$value] + 1 : 1; }); $sorted = $counts->sort(); $highestValue = $sorted->last(); return $sorted->filter(function ($value) use($highestValue) { return $value == $highestValue; })->sort()->keys()->all(); }
public function testWithMultipleModeValues() { $collection = new Collection([1, 2, 2, 1]); $this->assertEquals([1, 2], $collection->mode()); }