/
NikolayIvkin
/
TheAlgorithms
Обзор
Документация
Войти
/
NikolayIvkin
/
TheAlgorithms
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/com/thealgorithms/others/RemoveDuplicateFromString.java
30 строк
945 B
Alex Klymenko
refactor: `RemoveDuplicateFromString` (#5387)
25 авг 2024, 22:33
Не верифицирован
25 авг 2024, 22:33
f3851e3
Код
Авторство
О чём код?
package com.thealgorithms.others; /** * @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(); } }