Count Occurrences
PHP
Easy
7 views
Problem Description
Given n numbers and x, count how many times x appears.
Input Format
Line1 n x. Line2 n integers.
Output Format
One integer count.
Official Solution
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
$tokens=preg_split('/\\s+/', $inputText);
$i=0; $n=intval($tokens[$i++] ?? 0);
$x=intval($tokens[$i++] ?? 0);
$c=0;
for($k=0;$k<$n;$k++) if(intval($tokens[$i++] ?? 0)===$x) $c++;
echo $c;
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!