Check if List values are consecutive

前端 未结 12 1820
悲&欢浪女
悲&欢浪女 2021-02-01 05:46
List dansConList = new List();
dansConList[0] = 1;
dansConList[1] = 2;
dansConList[2] = 3;

List dansRandomList = new List

        
12条回答
  •  清歌不尽
    2021-02-01 06:29

    In order to check whether the series contain consecutive number or not you may use this

    Sample

    isRepeatable(121878999, 2);
    

    Result = True

    since 9 repeats two times , where upto is no of times in series

    isRepeatable(37302293, 3)
    

    Result = False

    since no number repeat 3 times in series

    static bool isRepeatable(int num1 ,int upto)
        {
            List myNo = new List();
            int previous =0;
            int series = 0;
            bool doesMatch = false;
            var intList = num1.ToString().Select(x => Convert.ToInt32(x.ToString())).ToList();
            for (int i = 0; i < intList.Count; i++)
            {
                if (myNo.Count==0)
                {
                    myNo.Add(intList[i]);
                    previous = intList[i];
                    series += 1;
                }
                else
                {
                    if (intList[i]==previous)
                    {
                        series += 1;
                        if (series==upto)
                        {
                            doesMatch = true;
                            break;
                        }
                    }
                    else
                    {
                        myNo = new List();
                        previous = 0;
                        series = 0;
                    }
                }
               
            }
    
            return doesMatch;
    
        }
    

提交回复
热议问题