Files
Java/src/main/java/com/thealgorithms/strings/RemoveDuplicateFromString.java
Deniz Altunkapan 6c24d27b03 Move methods from others to correct packages (#6475)
refactor: Move methods from `others` package to their respective packages
2025-08-18 22:28:19 +02:00

31 lines
946 B
Java

package com.thealgorithms.strings;
/**
* @author Varun Upadhyay (https://github.com/varunu28)
*/
public final class RemoveDuplicateFromString {
private RemoveDuplicateFromString() {
}
/**
* Removes duplicate characters from the given string.
*
* @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 input) {
if (input == null || input.isEmpty()) {
return input;
}
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 uniqueChars.toString();
}
}