JavaScript实现查找字符串中第一个不重复的字符
此算法仅供参考,小菜基本不懂高深的算法,只能用最朴实的思想去表达。
代码如下:
//找出字符串中第一个不重复的字符 // firstUniqueChar("vdctdvc"); --> t function firstUniqueChar(str){ var str = str || "", i = 0, k = "", _char = "", charMap = {}, result = {name: "",index: str.length}; for(i=0;i<str.length;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; }
小伙伴们如有更好的思路,还请告之一下,不胜感激
相关推荐
ILVNMM 2020-10-26
PinkBean 2020-08-19
Seandba 2020-08-16
徐建岗网络管理 2020-07-28
lynjay 2020-06-14
AaronPlay 2020-06-13
88384957 2020-06-12
herohope 2020-06-10
adwen00 2020-06-09
KilluaZoldyck 2020-06-06
hitxueliang 2020-06-05
lightindark 2020-06-03
baishuwei 2020-06-03
jediaellu 2020-05-31
twater000 2020-05-29
ItBJLan 2020-05-11
韩学敏 2020-05-09