Skip to content

Commit

Permalink
Make "removeDuplicatedCharacters" PSR-12 friendly and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
khouloudhaddad committed Nov 5, 2024
1 parent f406fa3 commit 37d62f9
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 12 deletions.
24 changes: 14 additions & 10 deletions Strings/RemoveDuplicatedCharacters.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
<?php

declare(strict_types=1);

/**
* The function loops through each character in the input string.
* It uses an array $seen to keep track of characters that have already been added to the output string.
* If a character hasn't been seen, it is appended to the result string, and the character is marked as seen.
* The function returns the modified string with duplicate characters removed.
* Removes duplicate characters from a string, retaining only the first occurrence of each character.
*
* @param string $inputString The input string from which duplicates will be removed.
* @return string The modified string with duplicate characters removed.
*/

function removeDuplicatedCharacters($inputString) {
function removeDuplicatedCharacters(string $inputString): string
{
// Initialize an empty array to keep track of seen characters
$seen = [];

// Initialize an empty string for the result
$result = '';

// Loop through each character in the input string
for ($i = 0; $i < strlen($inputString); $i++) {
$char = $inputString[$i];

// Check if the character has already been seen
if (!in_array($char, $seen)) {
if (!in_array($char, $seen, true)) {
// Add the character to the result and mark it as seen
$result .= $char;
$seen[] = $char;
}
}

return $result;
}
}
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^9",
"phpunit/phpunit": "^9.6",
"squizlabs/php_codesniffer": "^3.7"
},
"scripts": {
"test": "vendor/bin/phpunit tests"
}
}

71 changes: 71 additions & 0 deletions tests/Strings/RemoveDuplicatedCharactersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../Strings/RemoveDuplicatedCharacters.php';

use PHPUnit\Framework\TestCase;

/**
* Unit tests for the removeDuplicatedCharacters function.
* To run test: ./vendor/bin/phpunit tests/strings/RemoveDuplicatedCharactersTest.php
*/
final class RemoveDuplicatedCharactersTest extends TestCase
{
/**
* Test with a string that has multiple duplicates.
*/
public function testStringWithDuplicates(): void
{
$this->assertSame('progamin', removeDuplicatedCharacters('programming'));
}

/**
* Test with a string that has no duplicates.
*/
public function testStringWithoutDuplicates(): void
{
$this->assertSame('abc', removeDuplicatedCharacters('abc'));
}

/**
* Test with an empty string.
*/
public function testEmptyString(): void
{
$this->assertSame('', removeDuplicatedCharacters(''));
}

/**
* Test with a string containing only one character repeated.
*/
public function testSingleCharacterRepeated(): void
{
$this->assertSame('a', removeDuplicatedCharacters('aaaaa'));
}

/**
* Test with special characters.
*/
public function testStringWithSpecialCharacters(): void
{
$this->assertSame('ab!@', removeDuplicatedCharacters('aabb!!@@'));
}

/**
* Test with a string containing spaces.
*/
public function testStringWithSpaces(): void
{
$this->assertSame('a b', removeDuplicatedCharacters('a a a b b'));
}

/**
* Test with a string containing mixed case characters.
*/
public function testStringWithMixedCase(): void
{
$this->assertSame('aAbB', removeDuplicatedCharacters('aAaAaAbBbB'));
}
}

0 comments on commit 37d62f9

Please sign in to comment.