没有重复数字的数组 [英] array without duplicate numbers

查看:87
本文介绍了没有重复数字的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在运行时在数组中输入数字...没有重复值...如果你输入重复...数字是alredy那里输入另一个消息...数字必须在运行时输入... int [] arr = new int [] arr;

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;

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

推荐答案





我认为以下代码应该有效。只需检查用户是否多次输入重复值,然后检查不同值或重复值一次,然后检查正确的值。还有几个案例。



我试过这样的:



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();





希望这有帮助!! :)



问候,

Praneet



Hope this helps !! :)

Regards,
Praneet


考虑使用<$ c $而不是数组C> System.Collections.Generic.HashSet< INT>< / INT> 。它将保证添加元素的独特性;检查现有元素的时间复杂度将是O(1)(集合大小的渐近独立性)。



当填充完成后,您可以使用方法 ToArray 从集合中获取数组。



请参阅:

http://msdn.microsoft .com / zh-CN / library / bb359438%28v = vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/bb298736(v = vs.110)的.aspx [< a href =http://msdn.microsoft.com/en-us/library/bb298736(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]。



参见:

HTTP://msdn.microsoft.c om / en-us / library / bb298736(v = vs.110).aspx [ ^ ],

http://en.wikipedia.org/wiki/Time_complexity [ ^ ],

http://en.wikipedia.org/wiki/Computational_complexity_theory [ ^ ]。



-SA
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


取一个整数类型的临时列表

take one temp list which is of integer type
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...


这篇关于没有重复数字的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆