refactor: RemoveDuplicateFromString (#5387)

This commit is contained in:
Alex Klymenko 2024-08-25 21:33:41 +02:00 committed by GitHub
parent 101cb950ae
commit f3851e3adc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 55 additions and 28 deletions

View File

@ -1,8 +1,5 @@
package com.thealgorithms.others;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* @author Varun Upadhyay (https://github.com/varunu28)
*/
@ -10,38 +7,24 @@ public final class RemoveDuplicateFromString {
private RemoveDuplicateFromString() {
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inpStr = br.readLine();
System.out.println("Actual string is: " + inpStr);
System.out.println("String after removing duplicates: " + removeDuplicate(inpStr));
br.close();
}
/**
* This method produces a string after removing all the duplicate characters
* from input string and returns it Example: Input String - "aabbbccccddddd"
* Output String - "abcd"
* Removes duplicate characters from the given string.
*
* @param s String from which duplicate characters have to be removed
* @return string with only unique characters
* @param input The input string from which duplicate characters need to be removed.
* @return A string containing only unique characters from the input, in their original order.
*/
public static String removeDuplicate(String s) {
if (s == null || s.isEmpty()) {
return s;
public static String removeDuplicate(String input) {
if (input == null || input.isEmpty()) {
return input;
}
StringBuilder sb = new StringBuilder();
int n = s.length();
for (int i = 0; i < n; i++) {
if (sb.toString().indexOf(s.charAt(i)) == -1) {
sb.append(s.charAt(i));
StringBuilder uniqueChars = new StringBuilder();
for (char c : input.toCharArray()) {
if (uniqueChars.indexOf(String.valueOf(c)) == -1) {
uniqueChars.append(c); // Append character if it's not already in the StringBuilder
}
}
return sb.toString();
return uniqueChars.toString();
}
}

View File

@ -0,0 +1,44 @@
package com.thealgorithms.others;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.Test;
class RemoveDuplicateFromStringTest {
@Test
void testEmptyString() {
assertEquals("", RemoveDuplicateFromString.removeDuplicate(""));
}
@Test
void testNullString() {
assertNull(RemoveDuplicateFromString.removeDuplicate(null));
}
@Test
void testSingleCharacterString() {
assertEquals("a", RemoveDuplicateFromString.removeDuplicate("a"));
}
@Test
void testStringWithNoDuplicates() {
assertEquals("abc", RemoveDuplicateFromString.removeDuplicate("abc"));
}
@Test
void testStringWithDuplicates() {
assertEquals("abcd", RemoveDuplicateFromString.removeDuplicate("aabbbccccddddd"));
}
@Test
void testStringWithAllSameCharacters() {
assertEquals("a", RemoveDuplicateFromString.removeDuplicate("aaaaa"));
}
@Test
void testStringWithMixedCase() {
assertEquals("abAB", RemoveDuplicateFromString.removeDuplicate("aabABAB"));
}
}