Hi,
I think the following code should work. Just check that if user is entering duplicate values numerous times and then the distinct value or a duplicate value once and then the correct value. There can be a couple of more cases.
I have tried it like this:
int n=0, enteredNumber=0,index=0;
Console.WriteLine("Please enter the length of the array");
n = int.Parse(Console.ReadLine());
int[] arr = new int[n];
Console.WriteLine("Enter the elements of the array");
for (int i = 0; i < arr.Length; i++)
{
enteredNumber = int.Parse(Console.ReadLine());
index = Array.IndexOf(arr, enteredNumber);
if (index.Equals(-1))
{
arr[i] = enteredNumber;
}
else
{
Console.WriteLine("Duplicate. Please enter another no");
i--;
}
}
Console.WriteLine("The array you entered of length " + n + " is ");
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
Console.ReadKey();
Hope this helps !! :)
Regards,
Praneet