有一个表List<int>,希望获取某个整数i连续出现t次的次数(比如数字2连续出现3次的情况有几次),不知道用LINQ有没有办法实现?

解决方案 »

  1.   

      List<int> lst = new List<int>() ;
                var ary = Regex.Matches(string.Join(",", lst.Select(t => t.ToString()).ToArray()), @"2(,2(?=\D|$))+").Cast<Match>().Count(t => t.Value.Length - t.Value.Replace("2", "").Length == 3);
               
      

  2.   

    1楼的正则完全可以,给你个linq的,比较中规中矩            int[] arr = new int[] { 1, 2, 2, 2, 4, 5, 2, 2,2, 6 };
                int v = arr.Where((x, index) => index<arr.Length-3&&x == 2 && arr[index] == arr[index + 1] && arr[index + 1] == arr[index + 2]).Select(i => i).Count();
      

  3.   

    一楼的正则复制错了改为
    @"(?<=\D|^)2(,2(?=\D|$))+"
      

  4.   

    void Main()
    {
      int[] arr = new int[] { 1, 2, 2, 2, 4, 5, 2, 2, 2, 6 };
      List<List<int>> result=new List<List<int>>{new List<int>{arr[0]}};
      arr.Aggregate((x,y)=>
      { 
        if(x==result.Last().Last())

      if(x==y)  result.Last().Add(y);
      else  result.Add(new List<int>{y});  
    }
    else if(x==y) result.Add(new List<int>{x,y});
     return y;
      });
     // int c=result.Where(x=>x.Count()==3 && x.First()==2).Count();
     //Console.WriteLine(c);
     result.ForEach(x=>Console.WriteLine("{0} 出现了 {1}次",x.First(),x.Count()));
     /*
     1 出现了 1次
    2 出现了 3次
    4 出现了 1次
    5 出现了 1次
    2 出现了 3次
    6 出现了 1次
     */
    }
      

  5.   


    我真的膜拜bdmh,第一次见这么玩Linq的,Linq也搁了很长时间了。
      

  6.   

    http://blog.csdn.net/q107770540/article/details/6625243
      

  7.   


    以上是最接近我想要的,但我希望获取连续出现t次的次数,比如int[] arr = new int[] { 1, 2, 2, 2, 4, 5, 2, 2, 2, 6, 2 }中,2连续出现3次的情况有2次。
    这对于LINQ来说是不是太繁琐了?
      

  8.   


    谢谢!不过最主要还是希望用LINQ求解
      

  9.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] arr = new int[] { 1, 2, 2, 2, 4, 5, 2, 2, 2, 6 };
                int groupid = 0;
                var query = arr.Select((x, i) => new { x, group = (i == 0 ? groupid : (x == arr[i - 1] ? groupid : ++groupid)) })
                    .GroupBy(x => x.group)
                    .Select(x => string.Format("{0}连续{1}个", x.First().x, x.Count()))
                    .GroupBy(x => x).Select(x => x.First() + "有" + x.Count() + "次");
                foreach (string item in query)
                {
                    Console.WriteLine(item);
                }                
            }
        }
    }1连续1个有1次
    2连续3个有2次
    4连续1个有1次
    5连续1个有1次
    6连续1个有1次
    Press any key to continue . . .
      

  10.   


    另外督察的结果再做一次分组也可以实现。结果和我的一样。            var query = arr.Select((x, i) => new { x, group = (i == 0 ? groupid : (x == arr[i - 1] ? groupid : ++groupid)) })
                    .GroupBy(x => x.group)
                    .Select(x => string.Format("{0}连续{1}个", x.First().x, x.Count()))
                    //.GroupBy(x => x).Select(x => x.First() + "有" + x.Count() + "次");我的程序去掉最后一行和督察的等价
      

  11.   


    谢谢!这就是我想要的!
    看来LINQ真是潜力无穷,要好好学习学习才行!