Java Solution using HashMap


  • 0
    U
    public int firstUniqChar(String s) {
            HashMap<Character, Integer> map=new HashMap<>();
            char[] chars= s.toCharArray();
            int result=-1;
            for(char c: chars){
                if(map.containsKey(c)){
                    map.put(c, map.get(c)+1);
                }else{
                    map.put(c,1);
                }
            }
            
            for(int i=0;i<chars.length;i++){
                if(map.get(chars[i])==1){
                    result=i;
                    break;
                }
            }
            return result;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.