Last active
August 29, 2015 14:21
-
-
Save kamrankr/60009a2f0dde1b116f96 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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