惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Archives - TechRepublic
Security Archives - TechRepublic
O
OpenAI News
W
WeLiveSecurity
Hacker News: Ask HN
Hacker News: Ask HN
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Troy Hunt's Blog
L
LINUX DO - 最新话题
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
H
Heimdal Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Palo Alto Networks Blog
Project Zero
Project Zero
Attack and Defense Labs
Attack and Defense Labs
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Tor Project blog
Scott Helme
Scott Helme
T
Threat Research - Cisco Blogs
Simon Willison's Weblog
Simon Willison's Weblog
Spread Privacy
Spread Privacy
Cisco Talos Blog
Cisco Talos Blog
T
Threatpost
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
Google DeepMind News
Google DeepMind News
P
Privacy & Cybersecurity Law Blog
Know Your Adversary
Know Your Adversary
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
L
Lohrmann on Cybersecurity
Cloudbric
Cloudbric
I
Intezer
The Hacker News
The Hacker News
L
LINUX DO - 热门话题
AI
AI
B
Blog
S
Securelist
P
Proofpoint News Feed
量子位
Jina AI
Jina AI
V2EX - 技术
V2EX - 技术
T
The Exploit Database - CXSecurity.com
酷 壳 – CoolShell
酷 壳 – CoolShell
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
CERT Recently Published Vulnerability Notes
J
Java Code Geeks
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

博客园 - sun@live

两段代码 <收藏>提高Web性能的14条法则(详细版) MOSS与业务系统的集成 之 单点登录 MOSS与业务系统的集成 之 自定义Membership实现Forms方式验证 手机归属地数据—采集 .Net数据源自定义参数 JavaScript和CSS速查手册 序列化一个字符串到CDATA元素(.NET 1.1) Sandcastle工具SandcastleBuilder Windows Live Writer 写的一个双向选择器(JS) 论坛中,用户权限解决方法 (原创)一个改自java的代码统计工具 Web2.0用户注册,激活,密码找回模块 [学习日志]EyasBlog控件部分已基本完成-2005-12-03 学习日志(blog日历控件)-2005年11月12日 学习日志(Blog架构)-2005年11月09日 学习日志-2005年11月09日 学习计划-2005年11月07日
清除字符串数组中,重复元素 - sun@live - 博客园
sun@live · 2006-08-15 · via 博客园 - sun@live

清除字符串数组中,重复元素

2006-08-15 14:28  sun@live  阅读(1618)  评论()    收藏  举报

一、JS实现(收藏)

<script language="JavaScript">
<!--
var arrData=new Array();
for(var i=0; i<1000; i++)
{
arrData[arrData.length] 
= String.fromCharCode(Math.floor(Math.random()*26)+97);
}

//document.write(arrData+"<br/>"); 

//方法一,普通遍历
function myArray_Unique(myArray)
{
//var myArray=new Array("a","a","c","a","c","d","e","f","f","g","h","g","h","k");
var haha=myArray;
for(var i=0;i<myArray.length;i++)
{
for(var j=0;j<myArray.length;j++)
{
temp
=myArray[i];
if((i+j+1)<myArray.length&&temp==myArray[i+j+1]) //如果当前元素与后一个元素相等
haha.splice(i+j+1,1); //然后就移除下一个元素 
}

}

return haha;
}
 

//方法二
function getUnique(someArray)
{
tempArray
=someArray.slice(0);//复制数组到临时数组
for(var i=0;i<tempArray.length;i++)
{
for(var j=i+1;j<tempArray.length;)
{
if(tempArray[j]==tempArray[i])
//后面的元素若和待比较的相同,则删除并计数;
//
删除后,后面的元素会自动提前,所以指针j不移动
{
tempArray.splice(j,
1);
}

else
{
j
++;
}

//不同,则指针移动
}

}

return tempArray;
}
 

//方法三 正则表达式 -- 适用于字符型数组
function getUnique2(A)
{
var str = "\x0f"+ A.join("\x0f");
while(/(\w+)[^\1]*\1/.test(str))
str 
= str.replace("\x0f"+ RegExp.$1"");
return str.substr(1).split("\x0f");
}
 

//方法四 关联结构
Array.prototype.unique = array_unique;
function array_unique()
{
var o = new Object();
for (var i=0,j=0; i<this.length; i++)
{
if (typeof o[this[i]] == 'undefined')
{
o[
this[i]] = j++;
}

}

this.length = 0;
for (var key in o)
{
this[o[key]] = key;
}

return this;
}
 

var d = new Date().getTime();
document.write(myArray_Unique(arrData));
= new Date().getTime()-d;
document.write(
"<br/>2000元素 方法一算法计耗时 "+ d +" 毫秒!<br/><br/>"); //大约370ms~390ms左右 

var d = new Date().getTime();
document.write(getUnique(arrData));
= new Date().getTime()-d;
document.write(
"<br/>2000元素 方法二算法计耗时 "+ d +" 毫秒!<br/><br/>"); //大约360ms~380ms左右 

var d = new Date().getTime();
document.write(getUnique2(arrData));
= new Date().getTime()-d;
document.write(
"<br/>2000元素 正则表达式 方法三算法计耗时 "+ d +" 毫秒!<br/><br/>");//大约80ms左右 

var d = new Date().getTime();
document.write(arrData.unique());
= new Date().getTime()-d;
document.write(
"<br/>2000元素 关联结构 方法四算法计耗时 "+ d +" 毫秒!<br /><br />");//大约0ms~10ms左右 

//-->
</script> 

二、巧用.net的NameValueCollection实现(原创)

/// <summary>
/// 删除字符串组中相同元素
/// </summary>
/// <param name="strArr"></param>
/// <returns></returns>

public static string[] GetUnique(string[] strArr)
{
System.Collections.Specialized.NameValueCollection name 
= new System.Collections.Specialized.NameValueCollection(); 

foreach (string s in strArr)
{
name[s] 
= s;
}

return name.AllKeys;
}