我们如何在int数组中重复重复次数超过两次.我的一个例子如下
public static void main()
{
int[] array = { 3, 7, 9 , 7, 4, 9, 7 , 9}
Dictionary<int,int> dic = new Dictionary<int,int>();
foreach(var Val in array)
{
if(dict.containskey(Val)
dic[Val] ++;
else
dic[Val] = 1 ;
}
foreach (var pair in dic)
if(key.value > 2 )
Console.WriteLine(pair.key)
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点?在一次采访中我被问到了.我给出了上述被拒绝的解决方案.
编辑::我被要求编写一个内存较少且性能较好的程序.它不应该使用linq或任何内置的C#扩展方法...
对不起,如果问题有点混乱,我有一个场景,我需要两个获取包含 5 和 2 的所有数字。输入将是起始数字和结束数字,我需要找到这两个数字之间的所有数字,其中只包含数字 5 和数字2.
例如
Input - starting number - 1 end number -30
Output - 2,5,22,25.
Run Code Online (Sandbox Code Playgroud)
如何在 C# 中做到这一点?
编辑 :: 由于很少有用户问我尝试了什么,下面是我尝试过的示例片段,但我觉得这不是正确的方法。这将是昂贵的
public static void main()
{
int start = 1
int end = 30
for(int i = start; i<=end; i++)
{
if(CheckNumber(i))
{
Console.WriteLine(i);
}
}
}
static bool CheckNumber(int num)
{
int rem = 0;
while(num != 0)
{
rem = num % 10;
if(rem != 2 || rem!=5)
return false;
num = num/10; …Run Code Online (Sandbox Code Playgroud)