PHP Exercises: Print mode values from a given a sequence of integers
60. Mode Values from Sequence
Write a PHP program to print mode values from a given a sequence of integers. The mode value is the element which occurs most frequently. If there are several mode values, print them in ascending order.
Input:A sequence of integer’s ai (1 ≤ ai ≤ 100). The number of integers is less than or equals to 100.
Sample Solution:
PHP Code:
<?php
// Initialize an empty array to store input values
$hoge = array();
// Continue reading lines from standard input until an empty line is encountered
while ($hoge[] = trim(fgets(STDIN))) {}
// Count the occurrences of each unique value in the array
$hage = array_count_values($hoge);
// Find the maximum occurrence count
$max = max($hage);
// Find the keys (values) that have the maximum occurrence count
$koge = array_keys($hage, $max);
// Output the result
echo "Mode values (in ascending order):\n";
// Sort the keys in ascending order
sort($koge, SORT_ASC);
// Iterate through the sorted keys and output each value
foreach ($koge as $val) {
echo $val . PHP_EOL;
}
?>
Explanation:
- Initialize an Empty Array:
- An empty array named $hoge is initialized to store input values.
- Read Input Until Empty Line:
- A while loop reads lines from standard input using fgets(STDIN), trimming whitespace with trim().
- Each non-empty line is added to the $hoge array.
- The loop stops when an empty line is encountered.
- Count Occurrences of Each Value:
- The function array_count_values() is used to count the occurrences of each unique value in the $hoge array.
- The result is stored in the $hage associative array, where keys are the unique values and values are their counts.
- Find Maximum Occurrence Count:
- The max() function is used to find the highest occurrence count from the $hage array, stored in the variable $max.
- Get Keys with Maximum Occurrence:
- The function array_keys() retrieves all keys (unique values) from $hage that have the occurrence count equal to $max.
- The result is stored in the $koge array.
- Output the Result:
- A message "Mode values (in ascending order):" is printed to indicate what will be displayed next.
- Sort the Keys in Ascending Order:
- The sort() function is used to sort the $koge array in ascending order.
- Iterate and Output Each Mode Value:
- A foreach loop iterates through the sorted $koge array, printing each mode value on a new line.
Sample Input:
6
7
4
6
9
8
6
4
10
8
4
5
Sample Output:
Mode values (in ascending order): 4 6
Flowchart:

For more Practice: Solve these Related Problems:
- Write a PHP script to calculate the mode of a list of integers and output all values that appear most frequently.
- Write a PHP script to count the frequency of array elements and display the mode values in ascending order.
- Write a PHP function to determine mode values in an array while handling multiple modes correctly.
- Write a PHP script to filter a sequence for frequency counts and then output only the most frequent numbers sorted.
Go to:
PREV : Find Day of the Date in 2016.
NEXT : Most Frequent and Longest Word.
PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.