java 打印一字符中只出现一次的第一个字母

1. 

public class TestString {

public static void main(String args[]){
String a = "teststst";
for(int i = 0 ; i < a.length();i++){
char s = a.charAt(i);
if(a.indexOf(s) == a.lastIndexOf(s)){
System.out.println(s);
}
}
}

}



2.

public class TestString1 {
public void Cal(String s) {
int n = 0;
for(int i = 0 ; i < s.length();i++){
char t = s.charAt(i);
for(int j = 0 ; j < s.length();j++){
char tt = s.charAt(j);
if(t == tt){
n+=1;
}
}
if(n == 1){
System.out.println("out result: "+t);  
                 break;  
}
n = 0;
}


}
public static void main(String args[]) {
TestString1 cal = new TestString1();
cal.Cal("teterhrecd");


}


}



3. 

public class TestString2 {
public static void main(String args[]){
String inputStr = "teterhrecd";
HashMap hm = new HashMap();
char[] c = inputStr.toCharArray();
boolean flag = false;
for(int i = 0 ; i < c.length;i++){
for(int j = i+1; j<c.length;j++){
if(c[i]==c[j]){
flag = true;
hm.put(String.valueOf(c[i]),String.valueOf(c[i]));

break;
}
}
if(!flag && hm.get(String.valueOf(c[i]))== null){

 System.out.print(c[i]);  
              break;
}
flag = false;
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值