获取数组中指定重出现次数的元素集(实例)
如A1中有如下数值6 12 16 21 24 36 48 3 9 13 18 21 33 45 48 1 6 10 16 22 26 31 34 46 12 24 27 32 36 42 48 1 13 16 21 25 31 37 41 46 49 3 8 12 18 24 28 33 36 48 1 5 11 17 21 26 29 41
可按如下方式列出统计
【共00次】:02,04,07,14,15,19,20,23,30,35,38,39,40,43,44,47,
【共01次】:05,08,09,10,11,17,22,25,27,28,29,32,34,37,42,45,49,
【共02次】:03,06,13,18,26,31,33,41,46,
【共03次】:01,12,16,24,36,
【共04次】:21,48,
版块中有这样VB.NET贴,我粘贴到EXCEL中出错,烦请版主做一实例。在此感谢!!!
[解决办法]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s = "6 12 16 21 24 36 48 3 9 13 18 21 33 45 48 1 6 10 16 22 26 31 34 46 12 24 27 32 36 42 48 1 13 16 21 25 31 37 41 46 49 3 8 12 18 24 28 33 36 48 1 5 11 17 21 26 29 41";
var query = new string[] { string.Format("【共00次】:{0},", string.Join(",", Enumerable.Range(0, 50).Except(s.Split(' ').Select(x => int.Parse(x))).OrderBy(x => x).Select(x => x.ToString().PadLeft(2, '0')))) }.Union(s.Split(' ').Select(x => int.Parse(x))
.GroupBy(x => x)
.Select(x => new { x.Key, Count = x.Count() })
.GroupBy(x => x.Count)
.OrderBy(x => x.Key)
.Select(x => string.Format("【共{0}次】:{1},", x.Key.ToString().PadLeft(2, '0'), string.Join(",", x.Select(y => y.Key.ToString().PadLeft(2, '0')).OrderBy(y => y)))));
foreach (var item in query)
{
Console.WriteLine(item);
}
}
}
}
【共01次】:05,08,09,10,11,17,22,25,27,28,29,32,34,37,42,45,49,
【共02次】:03,06,13,18,26,31,33,41,46,
【共03次】:01,12,16,24,36,
【共04次】:21,48,
Press any key to continue . . .
[解决办法]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s = "6 12 16 21 24 36 48 3 9 13 18 21 33 45 48 1 6 10 16 22 26 31 34 46 12 24 27 32 36 42 48 1 13 16 21 25 31 37 41 46 49 3 8 12 18 24 28 33 36 48 1 5 11 17 21 26 29 41";
var query = new string[] { string.Format("【共00次】:{0},", string.Join(",", Enumerable.Range(1, 49).Except(s.Split(' ').Select(x => int.Parse(x))).OrderBy(x => x).Select(x => x.ToString().PadLeft(2, '0')))) }.Union(s.Split(' ').Select(x => int.Parse(x))
.GroupBy(x => x)
.Select(x => new { x.Key, Count = x.Count() })
.GroupBy(x => x.Count)
.OrderBy(x => x.Key)
.Select(x => string.Format("【共{0}次】:{1},", x.Key.ToString().PadLeft(2, '0'), string.Join(",", x.Select(y => y.Key.ToString().PadLeft(2, '0')).OrderBy(y => y)))));
foreach (var item in query)
{
Console.WriteLine(item);
}
}
}
}