C#实现检查数组中是否存在相同元素

1.代码(主函数)

static void Main(string[] args)
        {
            int[] a = { 1, 2, 3,3 };
            bool b = Check(a);
            Console.WriteLine(b);
        }
        private static bool Check(int[]Input)
        {
            //bool have=true;
            for(int i = 0; i< Input.Length;i++)
            {
                //int a = Input[i];
                for (int j = i+1;j

2.运行结果

C#实现检查数组中是否存在相同元素_第1张图片

 

设置数组a的值是{1,2,3,3}结果返回true,数组有重复值

C#实现检查数组中是否存在相同元素_第2张图片

 

设置数组a的值是{1,2,3,4}结果返回false,数组无重复值

3.原理

    遍历两遍数据,第一次取出数据第二次将数据与后面的数进行逐个比较,若有相同的返回结果true,若无,则返回false

你可能感兴趣的:(C#代码集合,c#,算法)