gpt4 book ai didi

java - 查找字符串中所有不重复的字符

转载 作者:行者123 更新时间:2023-12-01 11:17:59 25 4
gpt4 key购买 nike

你能帮我编写这个java程序吗?我有一个两个字符串,我需要确定哪些字符没有重复的字母。输入是:热爱生活输出将是:ovif

它将删除所有重复的字符。

这是我的代码,但它只打印并查找重复的字母。

public static void main(String[] args) {
Map<Character, Integer> map = new HashMap<Character, Integer>();

input = new Scanner(System.in);
String aw, bw;

System.out.println("Input First Word: ");
aw=input.next();
System.out.println("Input Second Word: ");
bw=input.next();
String s = aw+bw;
char[] chars = s.toCharArray();

for(Character ch:chars){
if(map.containsKey(ch)){
map.put(ch, map.get(ch)+1);
} else {
map.put(ch, 1);
}
}

Set<Character> keys = map.keySet();


for(Character ch:keys){
if(map.get(ch) > 1){
System.out.println(ch+" ");
}
}
}

我想打印在该程序中删除的字符。

最佳答案

for(Character ch:keys){
if(map.get(ch) == 1){
System.out.println(ch+" ");
}
}

将打印出所有出现过一次的字符。您将两个字符串中的每个字符都添加到映射中,因此仅出现一次的字符的值为 1。

关于java - 查找字符串中所有不重复的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31563522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com