Skip to content

Instantly share code, notes, and snippets.

@juque
Created January 17, 2025 15:07
Show Gist options
  • Save juque/1a0c170ae1eac34de6715d5c65e964f1 to your computer and use it in GitHub Desktop.
Save juque/1a0c170ae1eac34de6715d5c65e964f1 to your computer and use it in GitHub Desktop.
blog post: repeated character
<?php
// Blog post: "letra repetida site:juque.cl" <-- google it :)
function solution(string $s): string
{
$charFrequencies = [];
$inputArray = str_split($s);
$inputLength = count($inputArray);
for ($i = 0; $i < $inputLength; $i++) {
$char = $inputArray[$i];
if (!isset($charFrequencies[$char])) {
$charFrequencies[$char] = 0;
}
$charFrequencies[$char]++;
}
$maxFrequency = 0;
foreach ($charFrequencies as $freq) {
if ( $freq > $maxFrequency) {
$maxFrequency = $freq;
}
}
$result = '';
foreach ($charFrequencies as $char => $freq) {
if ($freq === $maxFrequency) {
$result = $char;
}
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment