LinkedHashMap solution


  • 0
    S

    class Solution {
    public int firstUniqChar(String s) {

        Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();
        
        for (int i = 0; i< s.length(); i++) {
            if(map.containsKey(s.charAt(i))) {
                map.put(s.charAt(i), map.get(s.charAt(i))+1);
            }else{
                map.put(s.charAt(i), 1);
            }
        }
               
        for(Map.Entry<Character, Integer> entry: map.entrySet()){
            if(entry.getValue() == 1){
                Character key = entry.getKey();
                return s.indexOf(key);
            }
        }       
        
        return -1;
               
    }
    

    }


Log in to reply
 

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