Skip to content

Instantly share code, notes, and snippets.

@Twoody
Created September 5, 2019 18:02
Show Gist options
  • Save Twoody/093a3763e64a4ee927bbe6d6c10361a7 to your computer and use it in GitHub Desktop.
Save Twoody/093a3763e64a4ee927bbe6d6c10361a7 to your computer and use it in GitHub Desktop.
Code Wars counting duplicates challenge done in PHP
<?php
function duplicateCount($text) {
$ret = 0;
$a = [];
$t = str_split( strtolower( $text ) );
$l = count($t);
for($i=0; $i<$l; $i++){
$c = $t[$i];
if ( array_key_exists($c, $t) ){
if( $t[$c] === 1 )
$ret += 1;
$t[$c] += 1;
}
else{
$t[$c] = 1;
}
}//end i-for
return $ret;
}
// PHPUnit Test Examples:
assert(0 == duplicateCount("") );
assert(0 == duplicateCount("abcde") );
assert(2 == duplicateCount("aabbcde") );
assert(2 == duplicateCount("aabBcde") ); //should ignore case
assert(1 == duplicateCount("Indivisibility") );
//Characters may not be adjacent
assert(2 == duplicateCount("Indivisibilities"));
?>
@behzodjon
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment