Skip to content

Commit f3851e3

Browse files
authored
refactor: RemoveDuplicateFromString (#5387)
1 parent 101cb95 commit f3851e3

File tree

2 files changed

+55
-28
lines changed

2 files changed

+55
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,30 @@
11
package com.thealgorithms.others;
22

3-
import java.io.BufferedReader;
4-
import java.io.InputStreamReader;
5-
63
/**
74
* @author Varun Upadhyay (https://github.com/varunu28)
85
*/
96
public final class RemoveDuplicateFromString {
107
private RemoveDuplicateFromString() {
118
}
129

13-
public static void main(String[] args) throws Exception {
14-
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15-
String inpStr = br.readLine();
16-
17-
System.out.println("Actual string is: " + inpStr);
18-
System.out.println("String after removing duplicates: " + removeDuplicate(inpStr));
19-
20-
br.close();
21-
}
22-
2310
/**
24-
* This method produces a string after removing all the duplicate characters
25-
* from input string and returns it Example: Input String - "aabbbccccddddd"
26-
* Output String - "abcd"
11+
* Removes duplicate characters from the given string.
2712
*
28-
* @param s String from which duplicate characters have to be removed
29-
* @return string with only unique characters
13+
* @param input The input string from which duplicate characters need to be removed.
14+
* @return A string containing only unique characters from the input, in their original order.
3015
*/
31-
public static String removeDuplicate(String s) {
32-
if (s == null || s.isEmpty()) {
33-
return s;
16+
public static String removeDuplicate(String input) {
17+
if (input == null || input.isEmpty()) {
18+
return input;
3419
}
3520

36-
StringBuilder sb = new StringBuilder();
37-
int n = s.length();
38-
39-
for (int i = 0; i < n; i++) {
40-
if (sb.toString().indexOf(s.charAt(i)) == -1) {
41-
sb.append(s.charAt(i));
21+
StringBuilder uniqueChars = new StringBuilder();
22+
for (char c : input.toCharArray()) {
23+
if (uniqueChars.indexOf(String.valueOf(c)) == -1) {
24+
uniqueChars.append(c); // Append character if it's not already in the StringBuilder
4225
}
4326
}
4427

45-
return sb.toString();
28+
return uniqueChars.toString();
4629
}
4730
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.thealgorithms.others;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class RemoveDuplicateFromStringTest {
9+
10+
@Test
11+
void testEmptyString() {
12+
assertEquals("", RemoveDuplicateFromString.removeDuplicate(""));
13+
}
14+
15+
@Test
16+
void testNullString() {
17+
assertNull(RemoveDuplicateFromString.removeDuplicate(null));
18+
}
19+
20+
@Test
21+
void testSingleCharacterString() {
22+
assertEquals("a", RemoveDuplicateFromString.removeDuplicate("a"));
23+
}
24+
25+
@Test
26+
void testStringWithNoDuplicates() {
27+
assertEquals("abc", RemoveDuplicateFromString.removeDuplicate("abc"));
28+
}
29+
30+
@Test
31+
void testStringWithDuplicates() {
32+
assertEquals("abcd", RemoveDuplicateFromString.removeDuplicate("aabbbccccddddd"));
33+
}
34+
35+
@Test
36+
void testStringWithAllSameCharacters() {
37+
assertEquals("a", RemoveDuplicateFromString.removeDuplicate("aaaaa"));
38+
}
39+
40+
@Test
41+
void testStringWithMixedCase() {
42+
assertEquals("abAB", RemoveDuplicateFromString.removeDuplicate("aabABAB"));
43+
}
44+
}

0 commit comments

Comments
 (0)