数组如何去除重复数据,只保留一条?

xwy5354 2012-03-29 11:41:32
例如一组数组中有
123456
123456
123456
123sdf
例如上面,我只想保留一条123456,请问如何做?
...全文
448 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
fqj715 2012-09-24
  • 打赏
  • 举报
回复
这个简介。一句话就实现了,谢谢。
[Quote=引用 1 楼 的回复:]

string[] strArr1 = { "123456", "123456", "123456", "123sdf", "123sdf" };
List<string> arr=strArr1.ToList().Distinct().ToList();
[/Quote]
fupx2008 2012-03-29
  • 打赏
  • 举报
回复
string[] strArr1 = { "123456", "123456", "123456", "123sdf", "123sdf" };
List<string> list = new List<string>();
foreach (string s in strArr1)
{
if (list.IndexOf(s) == -1)//如果不包含则添加
{
list.Add(s);
}
}
strArr1 = list.ToArray();
EnForGrass 2012-03-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

引用 1 楼 的回复:
string[] strArr1 = { "123456", "123456", "123456", "123sdf", "123sdf" };
List<string> arr=strArr1.ToList().Distinct().ToList();

那如果我的数组不确定个数呢?例如有很多很多呢
[/Quote]
搞不懂你要实现什么
色拉油 2012-03-29
  • 打赏
  • 举报
回复
你不能指望微软去实现你的需求,反正人家提供了Distinct方法,怎么用得看你的逻辑,这是你的事情
[Quote=引用 4 楼 的回复:]

引用 2 楼 的回复:

引用 1 楼 的回复:
string[] strArr1 = { "123456", "123456", "123456", "123sdf", "123sdf" };
List<string> arr=strArr1.ToList().Distinct().ToList();

那如果我的数组不确定个数呢?例如有很多很多呢

C# code

L……
[/Quote]
allen0118 2012-03-29
  • 打赏
  • 举报
回复
这是我判断ListBox里面是不是有重复的东西,如果有重复的就移除,你参考一下:


//循环判断ListBox是不是有重复的项,如果有就移除
for (int i = 0; i < ListBox1.Length; i++)
{
for (int j = i + 1; j < ListBox1.Length; j++)
{
if (ListBox1[j].ToString() == ListBox1[i].ToString())
{
ListBox1.Remove(j);
}
}
}
qxyywy 2012-03-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

引用 1 楼 的回复:
string[] strArr1 = { "123456", "123456", "123456", "123sdf", "123sdf" };
List<string> arr=strArr1.ToList().Distinct().ToList();

那如果我的数组不确定个数呢?例如有很多很多呢
[/Quote]

都一样的
若是你的数组存的结构或者泛型 就用List.Distinct的第二个重载方法就行了
http://blog.163.com/hr_test/blog/static/164852107201092062349140/
EnForGrass 2012-03-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

引用 1 楼 的回复:
string[] strArr1 = { "123456", "123456", "123456", "123sdf", "123sdf" };
List<string> arr=strArr1.ToList().Distinct().ToList();

那如果我的数组不确定个数呢?例如有很多很多呢
[/Quote]

List<string[]> list = new List<string[]>();
string[] strArr1 = { "123456", "123456", "123456", "123sdf", "123sdf" };
List<List<string>> arr = new List<List<string>>();
string[] strArr2 = { "12", "12", "16", "16", "10" };
string[] strArr3 = { "24", "24", "26" };
list.Add(strArr1);
list.Add(strArr2);
list.Add(strArr3);
foreach (string[] ss in list)
{
arr.Add(ss.ToList().Distinct().ToList());
}
cheng2005 2012-03-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

引用 1 楼 的回复:
string[] strArr1 = { "123456", "123456", "123456", "123sdf", "123sdf" };
List<string> arr=strArr1.ToList().Distinct().ToList();

那如果我的数组不确定个数呢?例如有很多很多呢
[/Quote]
先把数据结构整明白再说,我还没听说过 很多很多 这个结构。
xwy5354 2012-03-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
string[] strArr1 = { "123456", "123456", "123456", "123sdf", "123sdf" };
List<string> arr=strArr1.ToList().Distinct().ToList();
[/Quote]
那如果我的数组不确定个数呢?例如有很多很多呢
EnForGrass 2012-03-29
  • 打赏
  • 举报
回复
string[] strArr1 = { "123456", "123456", "123456", "123sdf", "123sdf" };
List<string> arr=strArr1.ToList().Distinct().ToList();
EnForGrass 2012-03-29
  • 打赏
  • 举报
回复
这样不就完了

string[] temp = File.ReadAllLines(@"C:\1.txt", Encoding.GetEncoding("GB2312"));
temp = temp.ToList().Distinct().ToArray();
File.WriteAllLines(@"C:\1.txt", temp);
xwy5354 2012-03-29
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
引用 2 楼 的回复:

引用 1 楼 的回复:
string[] strArr1 = { "123456", "123456", "123456", "123sdf", "123sdf" };
List<string> arr=strArr1.ToList().Distinct().ToList();

那如果我的数组不确定个数呢?例如有很多很多呢

搞不懂你要实现什么
[/Quote]
我说明白点了
我用streamreader读取一个TXT文件,文件里面的内容为:
12345
12345
12399
12385
12345
现在我想去除重复数据,也就是只剩下一条12345,把重复的去掉,是这个意思

110,568

社区成员

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

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

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