递归算法判断数组中是否有重复的项。

static private string[] array = { "aa", "bb","ee", "cc","abc","abc"};
        public static bool foo(string[] arr, int index)
        {
            if (arr == null && arr.Length == 0) {
                return false;
            }
            if (IsRepeat(arr, index))
            {
                return true;
            }
            else
            {
               return IsRepeat(arr, index + 1);
            }
        }

        public static bool IsRepeat(string[] arr, int index)
        {
            int count = 0;
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[index] == arr[i])
                {
                    count++;
                }
                if (count > 1)
                {
                    return true;
                }
            }
            return false;
        }



        static void Main(string[] args)
        {
            Console.WriteLine(foo(array, 0));
        }    

 

posted @ 2019-04-14 17:49  一叶青城  阅读(577)  评论(0)    收藏  举报