#387. First Unique Character in a String

ยท

1 min read

https://leetcode.com/problems/first-unique-character-in-a-string/?envType=daily-question&envId=2024-02-05

/**
 * @param {string} s
 * @return {number}
 */
var firstUniqChar = function (s) {
    const store = {}

    for (let char of s) {
        store[char] = (store[char] || 0) + 1
    }

    for (let [key, val] of Object.entries(store)) {
        if (val === 1) return s.indexOf(key) 
    }

    return -1
};

I solved this on my own.

ย