From 9db59b8aa8fa868d05402efa01ef09e8426b76ea Mon Sep 17 00:00:00 2001 From: alxkm Date: Sun, 25 Aug 2024 20:53:51 +0200 Subject: [PATCH 1/2] refactor: RemoveDuplicateFromString --- .../others/RemoveDuplicateFromString.java | 39 +++++----------- .../others/RemoveDuplicateFromStringTest.java | 45 +++++++++++++++++++ 2 files changed, 56 insertions(+), 28 deletions(-) create mode 100644 src/test/java/com/thealgorithms/others/RemoveDuplicateFromStringTest.java diff --git a/src/main/java/com/thealgorithms/others/RemoveDuplicateFromString.java b/src/main/java/com/thealgorithms/others/RemoveDuplicateFromString.java index 695a10648b6c..5b6466f6fa07 100644 --- a/src/main/java/com/thealgorithms/others/RemoveDuplicateFromString.java +++ b/src/main/java/com/thealgorithms/others/RemoveDuplicateFromString.java @@ -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(); } } diff --git a/src/test/java/com/thealgorithms/others/RemoveDuplicateFromStringTest.java b/src/test/java/com/thealgorithms/others/RemoveDuplicateFromStringTest.java new file mode 100644 index 000000000000..3721fb84678a --- /dev/null +++ b/src/test/java/com/thealgorithms/others/RemoveDuplicateFromStringTest.java @@ -0,0 +1,45 @@ +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")); + } +} + From 6513ebbed1c47efb2f1445b7b697035ca9e9885f Mon Sep 17 00:00:00 2001 From: alxkm Date: Sun, 25 Aug 2024 20:59:36 +0200 Subject: [PATCH 2/2] checkstyle: remove redundant line --- .../com/thealgorithms/others/RemoveDuplicateFromStringTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/others/RemoveDuplicateFromStringTest.java b/src/test/java/com/thealgorithms/others/RemoveDuplicateFromStringTest.java index 3721fb84678a..3401a51c56c9 100644 --- a/src/test/java/com/thealgorithms/others/RemoveDuplicateFromStringTest.java +++ b/src/test/java/com/thealgorithms/others/RemoveDuplicateFromStringTest.java @@ -42,4 +42,3 @@ void testStringWithMixedCase() { assertEquals("abAB", RemoveDuplicateFromString.removeDuplicate("aabABAB")); } } -