PHP Exercises: Print mode values from a given a sequence of integers
PHP: Exercise-60 with Solution
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:
PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a PHP program to that reads a date (from 2004/1/1 to 2004/12/31) and prints the day of the date. Jan. 1, 2004, is Friday. Note that 2004 is a leap year.
Two integers m and d separated by a single space in a line, m ,d represent the month and the day.
Next: Write a PHP program which reads a text (only alphabetical characters and spaces.) and prints two words. The first one is the word which is arise most frequently in the text. The second one is the word which has the maximum number of letters.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/php-exercises/php-basic-exercise-60.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics