Skip to content

Instantly share code, notes, and snippets.

@kamrankr
Last active August 29, 2015 14:21
Show Gist options
  • Save kamrankr/60009a2f0dde1b116f96 to your computer and use it in GitHub Desktop.
Save kamrankr/60009a2f0dde1b116f96 to your computer and use it in GitHub Desktop.
/*
* Write a function that will return the
* count of distinct case-insensitive alphabetic
* characters that occur more than once in the
* given string. The given string can be
* assumed to contain only uppercase and
* lowercase alphabets.
*
* @Input: a string
* @Output: the number of unique characters that appear more than once
*
* @Examples: duplicateCount("abcde") = 0 // No characters repeat
* duplicateCount("aabbcdeB") = 2 // 'a' and 'b' repeat
*/
echo duplicateCount("aabbcdeB");
function duplicateCount($text) {
$text = strtolower($text);
$duplicate = 0;
$checked = array();
for ($i=0; $i < strlen($text) ; $i++)
{
for ($j = $i+1; $j<strlen($text) ; $j++)
{
if ( $text[$i] == $text[$j] && !in_array($text[$i],$checked) )
{
$checked[] = $text[$i];
$duplicate++;
}
}
}
return $duplicate;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment