字串陣列去重

2021-08-02 08:54:09 字數 1059 閱讀 6069

這裡只考慮最簡單字串的陣列去重,暫不考慮,物件,函式,nan等情況,這種用正則實現起來就吃力不討好了。

非正則實現:es6實現

let str_arr=["b","c",1,"a", 3,"v", "2","e", "6","g", "9", "arr",9, "hello",",6", "9", "1", 2, 6, "b", "c"]

function

unique

(arr)

console.log(unique(str_arr))

es5實現

var str_arr = ["b","c",1,"a", 3,"v", "2","e", "6","g", "9", "arr",9, "hello",",6", "9", "1", 2, 6, "b", "c"]

function

unique

(arr) )

}console.log(unique(str_arr))

es3實現

var str_arr = ["b","c",1,"a", 3,"v", "2","e", "6","g", "9", "arr",9, "hello",",6", "9", "1", 2, 6, "b", "c"]

function

unique

(arr) ,

array =

for (var i = 0, len = arr.length; i < len; i++)

}return

array

}console.log(unique(str_arr))

用正則實現

var str_arr = ["b","c",1,"a", 3,"v", "2","e", "6","g", "9", "arr",9, "hello",",6", "9", "1", 2, 6, "b", "c"]

function unique(arr)

console.log(unique(str_arr))

js 陣列 字串 去重

array.prototype.unique1 function return n array.prototype.unique2 function r n為hash表,r為臨時陣列 for var i 0 i this.length i 遍歷當前陣列 return r array.prototyp...

2 陣列 字串去重

今天說的陣列和字串去重呢,主要用到es6新的資料結構set,它類似於陣列,但是成員的值都是唯一的,沒有重複的值,所以活用set來進行陣列和字串的去重。1.首先簡單介紹下set的用法 set本身是乙個建構函式,用來生成 set 資料結構。set函式可以接受乙個陣列 或者具有 iterable 介面的其...

字串去重

字串去重,思路是在乙個字串例如 strstrrtsiiiinnnggggg 中,遍歷所有的字元,拼接到stringbuffer中。在執行速度上來看stringbuffer的拼接速度要快與string。通過str.charat i 的方法得到當前遍歷到的字元。通過indexof方法得到該字元第一次出現...