手动调一下,只是判断有无重复的话, 可用一个两重循环搞定:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DupProject
{
class Program
{
static void Main(string[] args)
{
const int n = 5;
int[] a = new int[n];
for (int i = 0; i < n; i++)
{
Console.WriteLine("Please input the array, number from {0}-{1}",0, n-1);
a[i] = Convert.ToInt32(Console.ReadLine());
}
bool t = hasDup(a,n);
if (t)
{
Console.WriteLine("Found dup value");
}
else
Console.WriteLine("No dup value");
}
static bool hasDup(int[] a, int n)
{
for (int i = 0; i < n; i++)
{
while (a[i] != i)
{
int t = a[a[i]];
if (t != a[i])
{
a[a[i]] = a[i];
a[i] = t;
}
else
{
a[i] = -1;
return true;
}
}
}
return false ;
}
}
}