Home > Web Front-end > JS Tutorial > JavaScript implements finding the first non-repeating character in a string_javascript tips

JavaScript implements finding the first non-repeating character in a string_javascript tips

WBOY
Release: 2016-05-16 16:23:32
Original
1455 people have browsed it

This algorithm is for reference only. Xiao Cai basically doesn’t understand advanced algorithms and can only express it with the simplest thoughts.

Copy code The code is as follows:

//Find the first non-repeating character in the string
// firstUniqueChar("vdctdvc"); --> t
function firstUniqueChar(str){
var str = str || "",
i = 0,
        k = "",
​​​ _char = "",
charMap = {},
result = {name: "",index: str.length};
for(i=0;i _char = str.charAt(i);
If(charMap[_char] != undefined){
​​​ charMap[_char] = -1;
}else{
charMap[_char] = i;
}
}
for(k in charMap){
If(charMap[k]<0){
Continue;
}
If(result.index>charMap[k]){
        result.index = charMap[k];
       result.name = k;
}
}
Return result.name;
}

If you guys have any better ideas, please let me know, I’d be very grateful

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template