Click here to Skip to main content
15,868,292 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to enter numbers in array at runtime...without duplicate values..if you enter duplicate...number is alredy there enter another message...number must enter at runtime only...int[]arr=new int[]arr;
C#
for(int i=0;i<arr.length;i++)>
{
arr[i]=int.parse(console.readline());
}

..enter numbers in runtime without duplication in array..if you enter duplicate....number is already there ,enter another
Posted
Updated 20-Oct-14 18:56pm
v2

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:

C#
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
 
Share this answer
 
Instead of array, consider using System.Collections.Generic.HashSet<int></int>. It will guarantee uniqueness of the added elements; and the time complexity of checking up of the existing element will be O(1) (asymptotic independence of the size of the collection).

When population is complete, you can get an array from the collection using the method ToArray.

Please see:
http://msdn.microsoft.com/en-us/library/bb359438%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/bb298736(v=vs.110).aspx[^].

See also:
http://msdn.microsoft.com/en-us/library/bb298736(v=vs.110).aspx[^],
http://en.wikipedia.org/wiki/Time_complexity[^],
http://en.wikipedia.org/wiki/Computational_complexity_theory[^].

—SA
 
Share this answer
 
take one temp list which is of integer type
C#
List<int> temp=new List<int>();
for(int i=0;i<array.length;i++)>
{
int j =int.Parse(Console.ReadLine());

if(temp.Contains(i))
{
Console.WiteLine("Num already thr Enter another Num");
i--;
}
else
{
arr[i]=j;
temp.Add(j);
}
}


Dont know whether it works or not.... if works please mark it as answer...
 
Share this answer
 
v3
Arrary.Exists mehtod

http://msdn.microsoft.com/en-us/library/yw84x8be%28v=vs.110%29.aspx[^]

you can check weather it exists or not

Thank you,
 
Share this answer
 
Comments
aarif moh shaikh 21-Oct-14 1:13am    
Yes... That's Right

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900