I have a string of 10^1000 characters.where we cant run a loop to check one by one..then what should i do?
i do like
public class test {
public static void main(String[] args) {
String input = new String("abbc");
String output = new String();
for (int i = 0; i < input.length(); i++) {
for (int j = 0; j < output.length(); j++) {
if (input.charAt(i) != output.charAt(j)) {
output = output + input.charAt(i);
}
}
}
System.out.println(output);
}
}
I do it in this way to remove the all duplicate character from a string .but it is not efficient when the input.length() is too large.
You can try to convert the string to an array of char, and store it in a LinkedHashSet. That will preserve your ordering, and remove duplicates
char[] chars = string.toCharArray();
Set<Character> charSet = new LinkedHashSet<Character>();
for (char c : chars) {
charSet.add(c);
}
StringBuilder sb = new StringBuilder();
for (Character character : charSet) {
sb.append(character);
}
System.out.println(sb.toString());
If your String has separator I think that you could use a Set.
Set<String> set = new HashSet<String>(Arrays.asList(text.split(" ")));
Or in java 8:
Set<String> set = Arrays.stream(text.split(" ")).collect(Collectors.toSet());
You could maintain a set of characters ( Set<Character>
) you already did encounter for 0 .. (i-1).
As this reeks of homework I leave it to that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.