87,904
社区成员
发帖
与我相关
我的任务
分享
var array = [], str = str1 + ',' + str2;
str.replace(/([^,]*)/g, function($0, $1, i) {
if (str.indexOf($1) == i) array.push($1);
});
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<script type="text/javascript">
//CSDN seeu1688 提供的过滤数组重复元素算法
Array.prototype.unique = function() {
var a = {};
for (var i = 0; i < this.length; i++) {
if (typeof a[this[i]] == "undefined") a[this[i]] = 1;
}
this.length = 0;
for (var i in a) {
this[this.length] = i;
}
return this;
}
//修改 seeu1688 的算法
Array.prototype.iterate = function() {
var a = {};
var b = {};
for (var i = 0; i < this.length; i++) {
if (typeof a[this[i]] == "undefined") {
a[this[i]] = 1;
} else if (typeof b[this[i]] == "undefined") {
b[this[i]] = 1;
}
}
this.length = 0;
for (var i in b) {
this[this.length] = i;
}
return this;
}
String.prototype.iterate = function(str) {
arr = (str+","+this).split(",");
return arr.iterate().toString();
}
var array1 = new Array();
var array2 = new Array();
for (var i = 0; i < 2000; i++) {
array1[array1.length] = Math.floor(Math.random() * 2000);
array2[array2.length] = Math.floor(Math.random() * 2000);
}
var str1 = array1.unique().toString();
var str2 = array2.unique().toString();
document.write("array1 = " + array1 + "<br /><br/>");
document.write("array2 = " + array2 + "<br/><br/>");
//CSDN wcwtitxu 提供的算法
function itStr() {
str3 = ((str1 + "," + str2).match(/\b(\d+)\b(?=.*\b\1\b)/g) || []).join(",");
return str3;
}
var d = new Date().getTime();
document.write("array3 = " + str1.iterate(str2) + "<br /><br/>");
d = new Date().getTime() - d;
document.write("seeu1688 算法耗时:" + d + " 毫秒<br/><br/>");
var d = new Date().getTime();
document.write("array3 = " + itStr() + "<br /><br/>");
d = new Date().getTime() - d;
document.write("wcwtitxu 算法耗时:" + d + " 毫秒<br/><br/>");
</script>
</body>
</html>
var array1 = new Array();
var array2 = new Array();
for (var i = 0; i < 10000; i++) {
array1[array1.length] = Math.floor(Math.random() * 10000);
array2[array2.length] = Math.floor(Math.random() * 10000);
}
//数组中元素可重复
//返回2数组中重复元素所组成的新数组
//新数组中元素顺序以array1为准
Array.prototype.iterate = function(arr) {
var obj1 = {},obj2 = {},i,len,res = [];
len = this.length>arr.length?this.length:arr.length;
for(i=0; i < len; i++ ) {
obj1[this[i]] = true;
obj2[arr[i]] = true;
}
for(var t in obj1) if(obj2[t] === true) res.push(t);
return res;
}
//将字符串转成数组,调用Array.iterate(),返回的数组再转为字符串
String.prototype.iterate = function(str) {
return ((this.split(",")).iterate(str.split(","))).join(",");
}
//调用
document.write("String 1 = "+array1.toString()+"<br>");
document.write("String 2 = "+array2.toString()+"<br>");
var d = new Date().getTime();
document.write("new String = "+array1.iterate(array2)+"<br>");
d = new Date().getTime() - d;
document.write("Time: "+d+" ms<br />");
var str1 = '2,7,9,67,95,56,43,0,21';
var str2 = '8,9,5,98,34,7';
var str3 = ((str1+","+str2).match(/\b(\d+)\b(?=.*\b\1\b)/g)||[]).join(",");
alert(str3);
function iStr() {
var a={},res=[],b=str1.split(","),c=str2.split(","),i,len;
for (i=0,len=b.length; i<len; i++)a[b[i]]=true;
for (i=0,len=c.length; i<len; i++)if(a[c[i]]===true)res.push(c[i]);
return res.join(",");
}
function iStr() {
var a={},res=[],b=str1.split(","),c=str2.split(","),i,len;
for (i=0,len=b.length; i<len; i++)if(a[b[i]]!==true)a[b[i]]=true;
for (i=0,len=c.length; i<len; i++)if(a[c[i]]===true)res.push(c[i]);
return res.join(",");
}