我们可以先对数组进行排序,然后再比较。因为排序之后如果有相同的,那么相同的值肯定是挨着的,我们只需要在排序之后两两比较即可

static void Main(string[] args)
        {
            int[] nums = { 1, 1, 2,6,5,2,1 };
            var flag = containsDuplicate(nums);
        } 

static bool containsDuplicate(int[] nums)
        {
            Array.Sort(nums);
            for (int ind = 1; ind < nums.Length; ind++)
            {
                if (nums[ind] == nums[ind - 1])
                {
                    return true;
                }
            }
            return false;
        }

 

相关文章:

  • 2021-06-03
  • 2021-07-26
  • 2021-10-02
  • 2021-11-01
  • 2021-09-21
  • 2021-08-05
  • 2021-09-20
  • 2022-01-27
猜你喜欢
  • 2021-07-05
  • 2021-11-04
  • 2021-05-26
  • 2021-06-18
  • 2021-10-05
  • 2022-12-23
相关资源
相似解决方案