一、JS实现(收藏)
<script language="JavaScript">
<!--
var arrData=new Array();
for(var i=0; i<1000; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
arrData[arrData.length] = String.fromCharCode(Math.floor(Math.random()*26)+97);
}
//document.write(arrData+"<br/>");
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//方法一,普通遍历
function myArray_Unique(myArray)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
//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++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
for(var j=0;j<myArray.length;j++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
temp=myArray[i];
if((i+j+1)<myArray.length&&temp==myArray[i+j+1]) //如果当前元素与后一个元素相等
haha.splice(i+j+1,1); //然后就移除下一个元素
}
}
return haha;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//方法二
function getUnique(someArray)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
tempArray=someArray.slice(0);//复制数组到临时数组
for(var i=0;i<tempArray.length;i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
for(var j=i+1;j<tempArray.length;)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(tempArray[j]==tempArray[i])
//后面的元素若和待比较的相同,则删除并计数;
//删除后,后面的元素会自动提前,所以指针j不移动
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
tempArray.splice(j,1);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
j++;
}
//不同,则指针移动
}
}
return tempArray;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//方法三 正则表达式 -- 适用于字符型数组
function getUnique2(A)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
var str = "\x0f"+ A.join("\x0f");
while(/(\w+)[^\1]*\1/.test(str))
str = str.replace("\x0f"+ RegExp.$1, "");
return str.substr(1).split("\x0f");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//方法四 关联结构
Array.prototype.unique = array_unique;
function array_unique()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
var o = new Object();
for (var i=0,j=0; i<this.length; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if (typeof o[this[i]] == 'undefined')
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
o[this[i]] = j++;
}
}
this.length = 0;
for (var key in o)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
this[o[key]] = key;
}
return this;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
var d = new Date().getTime();
document.write(myArray_Unique(arrData));
d = new Date().getTime()-d;
document.write("<br/>2000元素 方法一算法计耗时 "+ d +" 毫秒!<br/><br/>"); //大约370ms~390ms左右
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
var d = new Date().getTime();
document.write(getUnique(arrData));
d = new Date().getTime()-d;
document.write("<br/>2000元素 方法二算法计耗时 "+ d +" 毫秒!<br/><br/>"); //大约360ms~380ms左右
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
var d = new Date().getTime();
document.write(getUnique2(arrData));
d = new Date().getTime()-d;
document.write("<br/>2000元素 正则表达式 方法三算法计耗时 "+ d +" 毫秒!<br/><br/>");//大约80ms左右
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
var d = new Date().getTime();
document.write(arrData.unique());
d = new Date().getTime()-d;
document.write("<br/>2000元素 关联结构 方法四算法计耗时 "+ d +" 毫秒!<br /><br />");//大约0ms~10ms左右
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//-->
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
二、巧用.net的NameValueCollection实现(原创)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// 删除字符串组中相同元素
/// </summary>
/// <param name="strArr"></param>
/// <returns></returns>
public static string[] GetUnique(string[] strArr)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
System.Collections.Specialized.NameValueCollection name = new System.Collections.Specialized.NameValueCollection();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (string s in strArr)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
name[s] = s;
}
return name.AllKeys;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)