首页 > 编程 > JavaScript > 正文

JavaScript实现找出字符串中第一个不重复的字符

2019-11-20 14:12:10
字体:
来源:转载
供稿:网友

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

//找出字符串中第一个不重复的字符// firstUniqueChar("vdctdvc"); --> tfunction 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;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表