取出数字数组中不重复出项的项

a5385987 2009-04-29 03:54:49
如(1,3,3,4,5,4) 应该得到1,5
...全文
251 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wenjie0728 2009-04-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ljhcy99 的回复:]
ArrayList list= new ArrayList();
int [] data = {1,3,3,4,5,4};
if(data.length >0)
list.Add(data[0].ToString());
foreach(int i in data)
{
if(!list.contains(i.tostring()))
{
list.Add(i.toString());
}
}

list 里面就是不重复数据
[/Quote]
这样不对 这样list里的值是:49 51 52 53

如下是值放在ArrayList 中:
System.Collections.ArrayList dd = new System.Collections.ArrayList();
System.Collections.ArrayList aa = new System.Collections.ArrayList();
dd.Add(1);
dd.Add(3);
dd.Add(3);
dd.Add(4);
dd.Add(5);
dd.Add(4);
dd.Add(9);
for (int i = 0; i < dd.Count; i++)
{
if (dd.LastIndexOf(dd[i]) == dd.IndexOf(dd[i])) //判断位置
{
aa.Add(dd[i]);
}
else
continue;
}
结果是:1 5 9
clark523 2009-04-29
  • 打赏
  • 举报
回复
试试下面的代码,它的耗时是2N

string[] str=new string[]{"1","2","5","5","3","4","7","7","9"}
List<string> list=new List<string>();
foreach(string temp in str)
{
list.add(temp);
}
Dictionary<string,int> dString=new Dictionary<string,int>();
foreach(string temp in list)
{
if(dString.containKey(temp))
{
dString[temp]++;
}else
{
dString.add(temp,0);
}
}
for(int i=list.count-1;i--;i>=0)
{
string temp=list[i];
if(dString[temp]>1)
{
list.remove(i);
}
}

这段代码运行后,list中剩下的就是楼主你想要的数据了
xv8844 2009-04-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ljhcy99 的回复:]
ArrayList list= new ArrayList();
int [] data = {1,3,3,4,5,4};
if(data.length >0)
list.Add(data[0].ToString());
foreach(int i in data)
{
if(!list.contains(i.tostring()))
{
list.Add(i.toString());
}
}

list 里面就是不重复数据
[/Quote]

C#经验交流群6624014期待您的加入,共同学习共同进步
ljhcy99 2009-04-29
  • 打赏
  • 举报
回复
ArrayList list= new ArrayList();
int [] data = {1,3,3,4,5,4};
if(data.length >0)
list.Add(data[0].ToString());
foreach(int i in data)
{
if(!list.contains(i.tostring()))
{
list.Add(i.toString());
}
}

list 里面就是不重复数据
yafeya 2009-04-29
  • 打赏
  • 举报
回复

// 去除所有重复的,显示1,3,4,5
private static int[] GetIntArrayWithoutRepeat(int[] paraArray)
{
ArrayList result = new ArrayList();
bool repeat = false;
for (int i = 0; i < paraArray.Length; i++)
{
for (int j = i + 1; j < paraArray.Length; j++)
{
if (paraArray[j] == paraArray[i])
{
repeat = true;
}
}
if(!repeat)
{
result.Add(paraArray[i]);
}
repeat = false;
}
return (int[])result.ToArray(typeof(int));
}


剔除所有重复的,显示1,5

private static int[] GetNoRepeat(int[] paraArray)
{
int repeattimes = 0;
ArrayList result = new ArrayList();
for (int i = 0; i < paraArray.Length; i++)
{
for (int j = paraArray.Length - 1; j >= 0; j--)
{
if (paraArray[i] == paraArray[j])
{
repeattimes++;
}
}
if (repeattimes == 1)
{
result.Add(paraArray[i]);
}
repeattimes = 0;
}
return (int[])result.ToArray(typeof(int));
}
LemIST 2009-04-29
  • 打赏
  • 举报
回复
ArrayList al = new ArrayList();
al.AddRange(new int[] { 7, 2, 8, 2, 3, 4, 2, 5, 6 });
al.Sort();
for (int i = 0; i < al.Count; i++)
{
int j = i + 1;
for (; j < al.Count && al[j].Equals(al[i]); j++) ;
if (j - i > 1)
al.RemoveRange(i--, j);
}
foreach (int i in al)
Console.WriteLine(i);
Garnett_KG 2009-04-29
  • 打赏
  • 举报
回复

// Linq

int[] ary = new int[] { 1, 3, 3, 4, 5, 4 };

var q = from x in ary
group x by x into Y
where Y.Count() == 1
select Y.Key;

foreach (var t in q)
{
Console.WriteLine(t);
}

jingsong2008 2009-04-29
  • 打赏
  • 举报
回复
if (str.compareTo(""))
str.Remove()

111,126

社区成员

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

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

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