Share my JavaScript O(n) code


  • 0
    E
    var firstUniqChar = function (s) {
        var map= {};
        for (var i = 0; i < s.length; i++) {
            if (map[s[i]] === undefined) {
                map[s[i]] = 0;
            } 
            map[s[i]]++;
        }
        for (var i = 0; i < s.length; i++) {
            if (map[s[i]] === 1) {
                return i;
            }
        }
        return -1;
    };
    

Log in to reply
 

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