如何去掉数组中重复的内容???在线等

udonome 2003-12-11 04:35:39
例如我有个数组{a,b,c,d,a,c},我想把它变成{a,b,c,d},该如何处理阿?
...全文
94 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
udonome 2003-12-12
  • 打赏
  • 举报
回复
用了cqing() 的方法,很简洁的。另外 jdxx(jdxx) 的方法虽然传统但也不见得效率差。 myhex(小乙) 不要那么激动嘛,大家各有各的方法,集思广益不是对自己对别人都有好处吗?
udonome 2003-12-12
  • 打赏
  • 举报
回复
晕,昨天到下班都没什么人回答,想不到晚上竟然有这么多热心人。好多方法啊,我不好一个个的测试,只好选择一个了。谢谢大家了,接帖。
gujianxin 2003-12-12
  • 打赏
  • 举报
回复
ArrayList al = new ArrayList();
for (int i = 0 ; i < 10 ; i++)
{
al.Add(i%6);
}

int nCount = al.Count;
bool[] bDel = new bool[nCount];

for(int i =0 ; i <nCount; i ++)
{
if(bDel[i])
continue;
for(int j = i+1 ; j < nCount ; j ++)
{
if(((int)al[j]) == ((int)al[i]))
bDel[j] = true;
}
}

for(int i = nCount - 1 ; i >=0; i --)
{
if(bDel[i])
al.RemoveAt(i);
}

cqing 2003-12-12
  • 打赏
  • 举报
回复
sorry, should be

public string[] removeDuplicate(string[] ArrInput)
{
ArrayList nStr=new ArrayList();
for(int i=0;i<ArrInput.Length;i++)
{
if(!nStr.Contains(ArrInput[i]))
{
nStr.Add(ArrInput[i]);
}
}
return nStr.ToArray(typeof(string));
}

you can change string to any object
cqing 2003-12-12
  • 打赏
  • 举报
回复
public string[] removeDuplicate(string[] ArrInput)
{
ArrayList nStr=new ArrayList();
for(int i=1;i<strArr.Length;i++)
{
if(!nStr.Contains(ArrInput[i]))
{
nStr.Add(ArrInput[i]);
}
}
return nStr.ToArray(typeof(string));
}

you can change string to any object
qqchen79 2003-12-12
  • 打赏
  • 举报
回复
>> 知道ArrayList的实质吗???链表唉!!!!!
No, ArrayList is still a consecutive memory block, not linked list.
myhex 2003-12-11
  • 打赏
  • 举报
回复
我真搞不懂,一个这么简单的算法,为什么就没人想动脑子写,都想着用现成的方法来实现呢,还有人用ArrayList,知道ArrayList的实质吗???链表唉!!!!!
连写进数据库再重新读出这么稀奇古怪的主意都有人列出来。。。

好在还有个 jdxx(jdxx)
13880079673 2003-12-11
  • 打赏
  • 举报
回复
该结贴了
13880079673 2003-12-11
  • 打赏
  • 举报
回复
private void button1_Click(object sender, System.EventArgs e)
{
string[] str1=new string[]{"s1","s2","s1","s3","s4","s5","s5","s6"};

ArrayList cc = new ArrayList();
for(int i = 0; i < str1.Length; ++i)
{
cc.Add(str1[i]);
}

ArrayList dd = new ArrayList();
for(int i = 0; i < cc.Count; ++i)
{
if(dd.BinarySearch(cc[i]) < 0)
{
dd.Add(cc[i]);
}
else
{
continue;
}
}

for(int i = 0; i < dd.Count; ++i)
{
MessageBox.Show((string)dd[i]);
}
}
rock1981 2003-12-11
  • 打赏
  • 举报
回复
装到Array做遍历
cuike519 2003-12-11
  • 打赏
  • 举报
回复
不好意思!我记错了!我说的集合不是数组!

Gloomybird 2003-12-11
  • 打赏
  • 举报
回复
我也是初学者。代码有什么不妥请指教。谢谢。
Gloomybird 2003-12-11
  • 打赏
  • 举报
回复
string[] str1=new string[]{"s1","s2","s1","s3","s4","s5","s5","s6","s6","s6","s8"};
ArrayList nStr=new ArrayList();
bool falg=true;
for(int i=1;i<str1.Length;i++)
{
for(int n=0;n<nStr.Count;n++)
{
if(str1[i]==nStr[n])
falg=false;
}
if(falg==true)
{nStr.Add(str1[i]);}
falg=true;
}
udonome 2003-12-11
  • 打赏
  • 举报
回复
这种集合操作有一个方法可以判断某元素是否存在在集合中!具体名称记不得了!你看看MSDN的帮助!


但是一定有这个方法!


到底是什么方法阿?
wincore 2003-12-11
  • 打赏
  • 举报
回复
非常高效率的办法:

全部存入数据库,然后用
select distinct field from tableName

jdxx 2003-12-11
  • 打赏
  • 举报
回复
有点小问题,可能,我写错了。
if (!bHaveRepeat )应该放在第二个for语句的外面。
for (int j=0;j<array.length;j++)中再加一个判断是不是自己的语句:
if (i!=j)
就可以了
jdxx 2003-12-11
  • 打赏
  • 举报
回复
arrar2就是最后你得到的数组
cuike519 2003-12-11
  • 打赏
  • 举报
回复
这种集合操作有一个方法可以判断某元素是否存在在集合中!具体名称记不得了!你看看MSDN的帮助!


但是一定有这个方法!
jdxx 2003-12-11
  • 打赏
  • 举报
回复
最笨的办法:做一个循环处理一下不就可以了

string tmp="";
bool bHaveRepeat=false;
int k=0;
for(int i=0;i<array.length;i++)
{
bHaveRepeat = false;
tmp=array[i];
for (int j=0;j<array.length;j++)
{
if (tmp==array[j])
{
bHaveRepeat = true;
break;
}
if (!bHaveRepeat )
{
arrar2[k]=tmp;
k++;
}
}
}
dewy61786 2003-12-11
  • 打赏
  • 举报
回复
用arraylist
加载更多回复(1)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧