refactor: RemoveDuplicateFromString
(#5387)
This commit is contained in:
parent
101cb950ae
commit
f3851e3adc
@ -1,8 +1,5 @@
|
|||||||
package com.thealgorithms.others;
|
package com.thealgorithms.others;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Varun Upadhyay (https://github.com/varunu28)
|
* @author Varun Upadhyay (https://github.com/varunu28)
|
||||||
*/
|
*/
|
||||||
@ -10,38 +7,24 @@ public final class RemoveDuplicateFromString {
|
|||||||
private 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
|
* Removes duplicate characters from the given string.
|
||||||
* from input string and returns it Example: Input String - "aabbbccccddddd"
|
|
||||||
* Output String - "abcd"
|
|
||||||
*
|
*
|
||||||
* @param s String from which duplicate characters have to be removed
|
* @param input The input string from which duplicate characters need to be removed.
|
||||||
* @return string with only unique characters
|
* @return A string containing only unique characters from the input, in their original order.
|
||||||
*/
|
*/
|
||||||
public static String removeDuplicate(String s) {
|
public static String removeDuplicate(String input) {
|
||||||
if (s == null || s.isEmpty()) {
|
if (input == null || input.isEmpty()) {
|
||||||
return s;
|
return input;
|
||||||
}
|
}
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder uniqueChars = new StringBuilder();
|
||||||
int n = s.length();
|
for (char c : input.toCharArray()) {
|
||||||
|
if (uniqueChars.indexOf(String.valueOf(c)) == -1) {
|
||||||
for (int i = 0; i < n; i++) {
|
uniqueChars.append(c); // Append character if it's not already in the StringBuilder
|
||||||
if (sb.toString().indexOf(s.charAt(i)) == -1) {
|
|
||||||
sb.append(s.charAt(i));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return sb.toString();
|
return uniqueChars.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user