JavaScript实现查找字符串中第一个不重复的字符_javascript技巧

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

此算法仅供参考,小菜基本不懂高深的算法,只能用最朴实的思想去表达。

复制代码代码如下:

//找出字符串中第一个不重复的字符
// 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] continue;
}
if(result.index>charMap[k]){
result.index = charMap[k];
result.name = k;
}
}
return result.name;
}

小伙伴们如有更好的思路,还请告之一下,不胜感激

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!