数组去重、字符串去重....
// 数组去重 var arr = [0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 'a', 'a'] Array.prototype.unique = function () { let temp = {}, newArr = [] for (let i = 0; i < this.length; i++) { if (!temp.hasOwnProperty(this[i])) { temp[this[i]] = this[i] newArr.push(this[i]) } } return newArr } console.log(arr.unique()) // [0, 1, 2, 3, "a"] // --------------------- 分隔线--------------------------- // 字符串去重 let str = '111222000aabb' String.prototype.unique = function () { let temp = {}, newStr = '' for (let i = 0; i < this.length; i++) { if (!temp.hasOwnProperty(this[i])) { temp[this[i]] = this[i] newStr += this[i] } } return newStr } console.log(str.unique()) //'120ab' // --------------------- 分隔线--------------------------- // 找出第一个出现次数为1次的字母 let str = 'tyuiatyuibtyuic' function test(str) { var temp = {} for (var i = 0; i < str.length; i++) { if (temp.hasOwnProperty(str[i])) { temp[str[i]]++ } else { temp[str[i]] = 1 } } for (var key in temp) { if (temp[key] === 1) { return key } } } console.log(test(str)); // 'a'