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

Remove duplicate characters from a string #178

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

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'));
}
}