题意:
获得第一个不重复的字符
解答:
本题使用map就够了,但是需要注意HashMap不是按顺序存放,所以遍历拿出来的并不是按原来加进去的顺序,此时可以使用LinkedHashMap,它是按顺序存放的。此外还需要注意遍历,Map.Entry<> :map.entrySet()
具体java代码如下:
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int N=in.nextInt();
LinkedHashMap<Integer,Integer> map=new LinkedHashMap();
int uni=0;
for(int i=0;i<N;i++){
int temp=in.nextInt();
if(map.get(temp)==null)
map.put(temp,1);
else{
map.put(temp,map.get(temp)+1);
}
}
for(Map.Entry<Integer,Integer> m:map.entrySet()){
if(m.getValue()==1){
uni=m.getKey();
break;
}
else{
uni=0;
}
}
if(uni==0)
System.out.println("None");
else
System.out.println(uni);
}
}