Skip to main content
  1. About
  2. For Teams
4 of 12
added 25 characters in body; deleted 1 character in body
NVRM
  • 13.4k
  • 2
  • 102
  • 101

Using PHP cli, a pre-filled reference array of iterators, a robust old tech for loop, descending sorting of values, so the most occurrence is the first key, since arsort() does maintain key association.

#!/usr/bin/php -d memory_limit=-1
<?php

$time_start = microtime(true);    // Benchmark

$list = file("1M_random_numbers.txt");
$count = count($list);
$temp = array_fill(0, $count, 0);

for ($i = 0; $i < $count; $i++){
  $v = (int)$list[$i];
  $temp[$v] = $temp[$v] + 1;
}

arsort($temp);

$result = array_key_first($temp);
echo "Most occurrence value : " . $result . PHP_EOL;
echo "Repeat count          : " . $temp[$result] . PHP_EOL;

$time_end = microtime(true);        // Benchmark
$time = $time_end - $time_start;    // Benchmark
echo "Runtime $time second(s)\n";   // Benchmark

Results on my machine (generation 5 i7 4.4Ghz) and PHP 8.4:

Most occurrence value : 142
Repeat count          : 1130
Runtime 0.23658084869385 second(s)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.