DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

First Unique Character in a String

var firstUniqChar = function(s) {
    const map={}
    for(let i=0; i<s.length; i++){
            map[s[i]]= (map[s[i]]||0)+1;
    }
// if want to return character Index;
    for(let i=0; i<s.length; i++){
           if(map[s[i]] === 1)
            return i
    }
// if you want to return character

 // for (let val in map) {
  //   if (map[val] === 1) {
  //     return val;
  //   }
  // }
    return -1

};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)