# Summary of String Array Reduplication Function Implemented by JS

Time：2019-9-28

In this paper, an example is given to illustrate the string array de-duplication function implemented by JS. Share for your reference, as follows:

Here we only consider the simplest string array to be de-duplicated, not to mention the object, function, NaN and so on. This kind of regular implementation is not satisfying.

Irregular implementation: ES6 implementation


<script>
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){
return [...new Set(arr)]
}
console.log(unique(str_arr))
</script>



Operation results: ES5 implementation


<script>
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) {
return arr.filter(function(ele, index, array) {
return array.indexOf(ele) === index
})
}
console.log(unique(str_arr))
</script>



Operational results are the same as above.

ES3 implementation


<script>
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) {
var obj = {},
array = []
for (var i = 0, len = arr.length; i < len; i++) {
var key = arr[i] + typeof arr[i]
if (!obj[key]) {
obj[key] = true
array.push(arr[i])
}
}
return array
}
console.log(unique(str_arr))
</script>



Operational results are the same as above.

Realization with regularization


<script>
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) {
return arr.sort().join(",,").
replace(/(,|^)([^,]+)(,,)+(,|$)/g, "$1$2$4").
replace(/,,+/g, ",").
replace(/,\$/, "").
split(",")
}
console.log(unique(str_arr))
</script>



Operation results: Interested friends can use itOnline HTML/CSS/JavaScript Code Running ToolHttp://tools.jb51.net/code/HtmlJsRun tests the performance of the above code.

PS: Here are some tools for your reference.

Online Repeated Item Removal Tool:
http://tools.jb51.net/code/quchong

Online Text Repetition Tool:
http://tools.jb51.net/aideddesign/txt_quchong

More information about JavaScript can also be found in this site: JavaScript Array Operation Skills Summary, JavaScript Character and String Operation Skills Summary, JavaScript Traversal Algorithms and Skills Summary, JavaScript Search Algorithms Skills Summary, JavaScript Mathematical Operation Usage Summary, JavaS Summary of Data Structure and Algorithmic Skills of cript and Summary of JavaScript Errors and Debugging Skills