return the first non repeating character in a string in javascript

后端 未结 26 2393
清酒与你
清酒与你 2020-12-30 09:04

So I tried looking for this in the search but the closest I could come is a similar answer in several different languages, I would like to use Javascript to do it.

T

26条回答
  •  执念已碎
    2020-12-30 09:33

    Here is my solution which have time complexity of o(n)

    function getfirstNonRepeatingCharacterInAString(params) {
        let count = {};
        for (let i = 0; i < params.length; i++) {
            let count1 = 0;
            if (!count[params.charAt(i)]) {
                count[params.charAt(i)] = count1 + 1;
            }
            else {
                count[params.charAt(i)] = count[params.charAt(i)] + 1;
            }
        }
        for (let key in count) {
            if (count[key] === 1) {
                return key;
            }
        }
        return null;
    }
    console.log(getfirstNonRepeatingCharacterInAString("GeeksfoGeeks"));
    

提交回复
热议问题