Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Strings/RemoveDuplicatedCharacters.php #180

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
* [Maxcharacter](./Strings/MaxCharacter.php)
* [Reversestring](./Strings/ReverseString.php)
* [Reversewords](./Strings/ReverseWords.php)
* [RemoveDuplicateCharacters](./Strings/RemoveDuplicateCharacters.php)

## Tests
* Ciphers
Expand Down
34 changes: 34 additions & 0 deletions Strings/RemoveDuplicateCharacters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/**
* 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 removeDuplicateCharacters(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, true)) {
// Add the character to the result and mark it as seen
$result .= $char;
$seen[] = $char;
}
}

return $result;
}

Comment on lines +33 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The linter wants you to remove the extra new line at the end of this file.

1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@
"test": "vendor/bin/phpunit tests"
}
}

71 changes: 71 additions & 0 deletions tests/Strings/RemoveDuplicateCharactersTest.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/RemoveDuplicateCharacters.php';

use PHPUnit\Framework\TestCase;

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

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

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

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

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

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file needs 1 empty new line at the end of the file. The other file needs just 1 but has two. 👍🏻

Loading