题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
import java.util.*; //HashSet保证字符不重复遍历,提高搜索速度 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s =in.nextLine(); HashSet<Character> hashSet = new HashSet<>(); String res = ""; for(int i=0; i<s.length(); i++){ if(hashSet.add(s.charAt(i))){ int temp = s.length() - s.replaceAll(s.charAt(i)+"","").length(); if(temp == 1){ System.out.print(s.charAt(i)); return; } } }System.out.print(-1); } }