Java程序在输出中打印重复字符而没有重复的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24553960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java program to print repeating characters in a string without duplicates in output
提问by Jayakumar A U
I am new to Java. Today I was trying to do a Java program to print duplicate characters in a string in which output also should not have duplicates. for example if string input is: "aabacdceefeg" output should not have repeating characters ie output should be: "ace" and should not not "aacee"
我是 Java 新手。今天我试图做一个 Java 程序来打印字符串中的重复字符,其中输出也不应该有重复。例如,如果字符串输入是:“aabacdceefeg”输出不应该有重复字符,即输出应该是:“ace”而不应该是“aacee”
public class programclass {
private static Scanner s;
public static void main(String [] args) {
String n, a[];
int i,j,l;
System.out.println("Enter the string: ");
s= new Scanner(System.in);
n=s.nextLine();
a=n.split("");
l = a.length;
for(i=0; i<l; i++){
for(j=i+1; j<l; j++){
if(a[i].equals(a[j])) {
System.out.println(a[i]);
}
}
}
}
}
Please help me to correct this out. Thanks in advance.
请帮我纠正这个问题。提前致谢。
采纳答案by Murali Krishna
You may want to use a Set for this to get the job done. here you go:
您可能希望为此使用 Set 来完成工作。干得好:
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.Set;
public class Sample {
private static Scanner s;
public static void main(String[] args) {
String n, a[];
int i, j, k, l;
System.out.println("Enter the string: ");
s = new Scanner(System.in);
n = s.nextLine();
a = n.split("");
l = a.length;
Set<String> noDupes = new LinkedHashSet<>();
StringBuilder sb = new StringBuilder();
for (i = 0; i < l; i++) {
noDupes.add(a[i]);
}
for (Iterator<String> it = noDupes.iterator(); it.hasNext();) {
String f = it.next();
sb.append(f);
}
System.out.println(sb.toString());
}
}
回答by meanderingmoose
I think a better strategy here would be to save each used character in a set and then check to see if the letter is in the set.
我认为这里更好的策略是将每个使用过的字符保存在一个集合中,然后检查该字母是否在集合中。
public class programclass {
private static Scanner s;
public static void main(String [] args) {
String n, a[];
int i,j,k,l;
Set<String> set = new HashSet<String>();
System.out.println("Enter the string: ");
s= new Scanner(System.in);
n=s.nextLine();
a=n.split("");
l = a.length;
for(i=0; i<l; i++){
if(set.contains(a[i])){
System.out.println(a[i]);
}
set.add(a[i]);
}
} }
} }
If you only want a repeated character to print out once, add another set to keep track of letters which have already been printed.
如果您只想将重复的字符打印一次,请添加另一组以跟踪已打印的字母。
回答by Robby Cornelissen
Here's a solution that sorts the string and then uses a regular expression to remove the duplicates:
这是一个对字符串进行排序然后使用正则表达式删除重复项的解决方案:
String string = "aabacdceefeg";
char[] chars = string.toCharArray();
Arrays.sort(chars);
String sorted = new String(chars);
String result = sorted.replaceAll("(.)\1+", "");