asp.net 获取数组中重复数据代码
asp教程.net 获取数组中重复数据代码
下面提供了五款不同方法的取出数据中重复的数据并统计重复的次数。
dictionary<int, int> dic = new dictionary();
int[] arr = new int[] { 1, 5, 2, 6, 7, 1, 5, 4, 1, 6, 8, 7, 6 };
foreach(int i in arr)
{
try
{
dic.add(i, i);
}
catch(exception err)
{
//.... ...
}
}
//方法二
int[] arr = new int[] { 1, 5, 2, 6, 7, 1, 5, 4, 1, 6, 8, 7, 6 };
int n = arr.length - arr.distinct().count();
//方法三
int[] ary = new int[] { 1, 3, 3, 4, 5, 4 };
var q = from x in ary
group x by x into y
select {y.key,y.count()};
//方法四
int[] arr = new int[] { 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7 };
var gs = arr.groupby(i => i).orderby(i => i.count());
console.writeline(gs.lastordefault().toarray()[0]);
//方法五
int[] arr = { 1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5 };
var linqer =
from a in arr
group arr by a into b
orderby b.count()
select new
{
b.key,
count = b.count()
};
foreach (var i in linqer)
{
console.writeline("{0}出现{1}次", i.key, i.count);
}
补充:asp.net教程,.Net开发