Skip to content

refactor: RemoveDuplicateFromString #5387

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

Merged
merged 2 commits into from
Aug 25, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,47 +1,30 @@
package com.thealgorithms.others;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
* @author Varun Upadhyay (https://github.com/varunu28)
*/
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();
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}